Ryan Sampson Ryan Sampson - 3 months ago 22
ASP.NET (C#) Question

ASP.NET exception "Thread was being aborted" causes method to exit

In the code below, sometimes someFunctionCall() generates a "Thread was being aborted" exception. 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?

Thanks.

public void method()
{
// CODE BLOCK A
//...


try
{
someFunctionCall(); // this call is generating thread abort exception
}
catch(Exception ex)
{
// log exception message
}

// CODE BLOCK B
// ...

}

Answer

This is a ThreadAbortException; it's a special exception that is automatically rethrown at the end of every catch block, unless you call Thread.ResetAbort().

ASP .Net methods like Response.End or Response.Redirect (unless you pass false) throw this exception to end processing of the current page; your someFunctionCall() is probably calling one of those methods.

ASP .Net itself handles this exception and calls ResetAbort to continue processing.