In the code below, sometimes someFunctionCall()
generates an exception:
Thread was being aborted.
How come the code in code Block B never runs? Does ASP.NET start a new thread for each method call? I was suprised to see that when this exception happens the code in Block B never runs, the method returns, and my application keeps running. Can someone please explain this?
public void method()
{
// CODE BLOCK A
//...
try
{
someFunctionCall(); // this call is generating thread abort exception
}
catch(Exception ex)
{
// log exception message
}
// CODE BLOCK B
// ...
}
Best Solution
This is a
ThreadAbortException
; it's a special exception that is automatically rethrown at the end of every catch block, unless you callThread.ResetAbort()
.ASP .Net methods like
Response.End
orResponse.Redirect
(unless you passfalse
) throw this exception to end processing of the current page; yoursomeFunctionCall()
is probably calling one of those methods.ASP .Net itself handles this exception and calls
ResetAbort
to continue processing.