Jan 2, 2008

Exception : thread was being aborted / mscorlib

I got this message while debugging in asp.net 2.0 at this
Response.redirect("myABCfile.aspx")


I was thinking what's wrong with this line and you will get this exception if you throw the exception out to the display only.

Surfing around and I got the answer.
We have to understand how response.write or server.transfer work!

In ASP.NET, response.write and server.transfer will end the threading with response.end.
Thus, if we have a TRY...CATCH...END TRY within it, the TRY Thread will be aborted and ended suddenly and the exception will be caught.

We should make the response.redirect before or outside the catch at this point.

Usually, we will have a checking for session lost at the beginning of the page.

If String.IsNullOrEmpty(Session("user_id")) Then
Response.redirect("myLoginPage.aspx")
End if


This simple validation portion might not need to be inside Try Catch loop and you will be safe!

In addition, sometimes programming like to declare variable inside Try Catch loop too, and it is NOT necessary to do so.

A simple statement likes
Dim myString as string
is NOT necessary to be in Try Catch loop. :)

No comments:

Post a Comment