In this case, the response.write in the catch actually executes, but you never get to see it on the browser because the redirection header has been written already during the redirect execution. And thus it still redirects to google. What happens
is: - write redirection header for google - try to terminate thread -> oops, exception here - response.write - response sent to the client (with "Catch as Catch can" inside), which redirects to google as instructed in the header Hope this clarifies things.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Be careful, Server.Transfer does not take an arbitrary url. It is very different from redirect in that it executes the target page internally, without redirecting client-side as redirect does. Still, it is interesting to note that Server.Transfer is just Server.Execute,
followed by... Response.End of course. Which explains a lot. If you don't want the End to execute when Server.Transfering, just use Server.Execute instead. To sum things up, I think there is nothing mysterious left here. Let me know if anything still looks
unclear to you. (BTW, JB it happens that I used to be a theoretical physicist, and there is nothing wrong with being a control freak in this area either ;) )
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Bertrand, 1. This one's tricky. To understand what happens, you have to know how redirect works. Basically, it adds a redirection header to the response, and it calls Response.End. This is where you get all the trouble from: End is really aborting the thread,
which is where you get a ThreadAbortException. It's probably this exception you're catching when redirecting. And of course, the second redirect wins... If your statement is 'true' for Code 1, then that's how ASP.NET implemented 'HTTP Response.Redirect'. which
is OK according to MS, but it's still questionable from the HTTP Specification wise in the sense Tomcat server did not implement the same way - See Posted: 31 Jul 2003 01:30 PM, Code 4. Again, every implementer has their own right and justification for their
environment. No question here. However, from HTTP Specification point of view, there seemed to be missing something here for a customer of a tool. See Posted: 31 Jul 2003 01:30 PM. 2. ::Let me know if anything still looks unclear to you. Any thought about
the HTTP Specification point of view?
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Hi, I'm not sure I'm following you on this one. As far as I know, the Http specification just states how the redirection headers and response status work. Why would it say anything about exceptions? Not all application servers even throw exceptions in the first
place. IIS and ASP.NET seem to me to be in perfect accordance with the Http protocol. I see nothing questionable from the specs point of vue. Tomcat IS NOT the Http specification. They implemented redirect without exception, but that's their choice. Perhaps
I should point out that the versions of Redirect that throws does so to be as close as possible to the corresponding behavior in classic ASP. In fact, it is there only for compatibility purposes, and in ASP.NET you should really always use the second form,
with the boolean parameter set to false. And you may also prefer Execute which does not involve a server roundtrip or exception. Once more, I don't see where we're walking away from specs here. But then again, I may be missing something.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Phuoc Bertrand, I'm still a little unclear about the timing. 1.
::This one's tricky. To understand what happens, you have to know how redirect works. ::Basically, it adds a redirection header to the response, and it calls Response.End. This is ::where you get all the trouble from: End is really aborting the thread, which
is where you ::get a ThreadAbortException. It's probably this exception you're catching when redirecting. ::And of course, the second redirect wins... If 1 above is true, this is what is happening in the Try Block... (
Response.Redirect("http://www.google.com")
Response.End '## The program should end here! Nothing else should get to the response buffer.
This is why I don't understand this sentence,(::And of course, the second redirect wins... )
If Response.End is called, how does the second redirect ever get written? The whole thing should have ended....
Or are you saying that you can't explcitly Response.End out of a try catch block? Such that the try catch block overides the Response.End temporarily...
I just tried this and sure enough I can't response.End out of the block in either the try or the catch. Both URL's get written.
Sub Page_Load(sender as object, e as eventargs)
try
Response.write("http://www.google.com")
Response.End
catch ex as exception
Response.End
Response.write("http://www.asp.net")
End Try
End Sub
However, didn't phuoc say that this
behalves differently on Tomcat? I would be interested in seeing what the above code would do on the Tomcat server -- Hint, Hint! ;-) , ;-) nudge, nudge...
say no more.... Here is my new and improved thinking, please tell me if I am wrong.....
You can not Response.End out of a Try Catch Block EVER! :-D JB
John Belthoff
Dodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
bleroy, 1. ::Tomcat IS NOT the Http specification Correct. It's a server like IIS. And that's what I said "3. Code 4: JSP and Tomcat Server 4.x/Jdk1.4.x " Even JB said: Very interesting how it works on a Tomcat Server. 2. ::Why would it say anything about exceptions?
Not all application servers even throw exceptions in the first place ::They implemented redirect without exception, but that's their choice. Correct. And that's what I said : "Again, every implementer has their own right and justification for their environment.
No question here. " 3. ::Perhaps I should point out that the versions of Redirect that throws does so to be as close as possible to the corresponding behavior in classic ASP. In fact, it is there only for compatibility purposes, and in ASP.NET you should really
always use the second form, with the boolean parameter set to false. And you may also prefer Execute which does not involve a server roundtrip or exception. Now, without telling ASP.NET team members in advance, if you conduct a virtual test with 10 ASP.NET
team members with Code 1, you will be surprised how many of them will pick the try part which is google. (which is the wrong answer according to the 'test' only). 'Apparently' and on the surface, every one will pick 'google' immediately. If you look again
the other thread, you will see 2 people picked 'google' and found out the 'asp.net' is correct by your explaination. Google is intuively the right answer but it's not due to your explanation. But then how many know that *Trick*. By that *Trick*, I meant something
is missing here. I hope what I said is clear. 4. Is it possible that you post the MSIL code generated by Response.Redirect (url) ? is indeed : Response.Redirect(url), Response.End
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Hi, Agreed, it's tricky. But the compatibility issue is a much bigger one than the potential confusion caused by this. After all, why do you write Response.Redirect? It's to redirect to another page AND stop all processing of the current one. So putting it
in a try/catch block around it does not make that much sense most of the time. Most people would get case ! wrong, agreed, but to me it's a little similar to these unreadable pieces of C you find in some textbooks as exercises (whereas they should be pointed
out as the thing not to do under any circumstance). I mean that it's artificial and very marginal. You may have legitimately stumbled on one of the few cases where it really looks like a bug. Bad luck here. When designing something like ASP.NET, you've got
to make decisions and they may look legitimate to some, and arbitrary to others. I don't think you need to see the MSIL. If you really really need to see it to believe me, you can use ILDASM on System.Web.dll. It should work: we don't obfuscate our code as
far as I know. I'm sorry if this caused you any inconvenience.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi again, I posted about an hour ago but is has not showed up, but is this what I am supposed to think.....?
<script runat="server">
Sub Page_Load(sender as object, e as eventargs)
Try
Response.Write( "Hello from try! " )
Response.End
Catch
Response.End
Response.Write( "Hello from catch! " )
Finally
Response.End
Dim strString As StringBuilder
strString = New StringBuilder( "Finally! " )
strString.Append( "" )
strString.Append( vbNewLine )
strString.Append( "You can never... ever... and I do mean never...! " )
strString.Append( "Response.End out of a Try Catch Block!" )
Response.Write( strString.ToString )
End Try
End SUb
</script>
:-D JB
John Belthoff
Dodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
bleroy, 1. ::So putting it in a try/catch block around it does not make that much sense most of the time. Ask ASP.NET Testing Unit to see how they set up their Test cases to see :-D ::You may have legitimately stumbled on one of the few cases where it really
looks like a bug. Bad luck here. When designing something like ASP.NET, you've got to make decisions and they may look legitimate to some, and arbitrary to others. We trust every body (MS, ORACLE, IBM, etc...) but every body has service pack too :-D Just kidding!
::If you really really need to see it to believe me, you can use ILDASM on System.Web.dll. It should work: we don't obfuscate our code as far as I know. Religiously speaking believing is seeing, but scientifically seeing is believing. :-D We trust you. ::I'm
sorry if this caused you any inconvenience. We too for being so curious about this point. :-D 2. Again, 1 Million thanks for all your speedy service! unless JB has something else to say.
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Damn it. It wasted me 3 hours while I was examining my code. I couldn't even imagine that Response.End() could throw an exception. It's my problem though that I didn't read the doc first, I just didn't check the Response.End doc. The implementation is out of
my mind. What I did is: string mycontent = "dsdksjalkjsa"; Response.Write(mycontent); Response.End(); You can never imagine that this code won't work!!!!! It actually break the programming model! The behavior is so stupid, that I think MS should say this in
any ASP.NET introduction, I believe a lot of programmer would be caught by this, Calvin
bleroy
All-Star
15617 Points
2302 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 07:44 PM|LINK
private void Page_Load(object sender, System.EventArgs e) { try { Response.Redirect("http://www.google.com"); } catch (Exception ex) { Response.Write("Catch as Catch Can!"); } }In this case, the response.write in the catch actually executes, but you never get to see it on the browser because the redirection header has been written already during the redirect execution. And thus it still redirects to google. What happens is: - write redirection header for google - try to terminate thread -> oops, exception here - response.write - response sent to the client (with "Catch as Catch can" inside), which redirects to google as instructed in the header Hope this clarifies things.----
This posting is provided "AS IS" with no warranties, and confers no rights.
bleroy
All-Star
15617 Points
2302 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 07:50 PM|LINK
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 08:44 PM|LINK
and where there is a team, there is more than one way.
bleroy
All-Star
15617 Points
2302 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 09:22 PM|LINK
----
This posting is provided "AS IS" with no warranties, and confers no rights.
JBelthoff
Member
553 Points
135 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 09:34 PM|LINK
Response.Redirect("http://www.google.com") Response.End '## The program should end here! Nothing else should get to the response buffer.This is why I don't understand this sentence,(::And of course, the second redirect wins... ) If Response.End is called, how does the second redirect ever get written? The whole thing should have ended.... Or are you saying that you can't explcitly Response.End out of a try catch block? Such that the try catch block overides the Response.End temporarily... I just tried this and sure enough I can't response.End out of the block in either the try or the catch. Both URL's get written.Sub Page_Load(sender as object, e as eventargs) try Response.write("http://www.google.com") Response.End catch ex as exception Response.End Response.write("http://www.asp.net") End Try End SubHowever, didn't phuoc say that this behalves differently on Tomcat? I would be interested in seeing what the above code would do on the Tomcat server -- Hint, Hint! ;-) , ;-) nudge, nudge... say no more.... Here is my new and improved thinking, please tell me if I am wrong..... You can not Response.End out of a Try Catch Block EVER! :-D JBDodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 10:18 PM|LINK
and where there is a team, there is more than one way.
bleroy
All-Star
15617 Points
2302 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 10:35 PM|LINK
----
This posting is provided "AS IS" with no warranties, and confers no rights.
JBelthoff
Member
553 Points
135 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 10:41 PM|LINK
<script runat="server"> Sub Page_Load(sender as object, e as eventargs) Try Response.Write( "Hello from try! " ) Response.End Catch Response.End Response.Write( "Hello from catch! " ) Finally Response.End Dim strString As StringBuilder strString = New StringBuilder( "Finally! " ) strString.Append( "" ) strString.Append( vbNewLine ) strString.Append( "You can never... ever... and I do mean never...! " ) strString.Append( "Response.End out of a Try Catch Block!" ) Response.Write( strString.ToString ) End Try End SUb </script>:-D JBDodge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Phuoc
Participant
1150 Points
230 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 10:51 PM|LINK
and where there is a team, there is more than one way.
cklein
Participant
1561 Points
363 Posts
Re: Catch me if you can? by try/catch/response.redirect. Need expert to explain.
Jul 31, 2003 10:52 PM|LINK
http://www.raincoder.com
Equal parts art and science
Email: cguo@raincoder.com