Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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     // ...     } 
like image 764
Ryan Sampson Avatar asked Jun 18 '09 18:06

Ryan Sampson


People also ask

How to avoid thread abort exception in c#?

ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread.

What causes thread abort exception?

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException on . NET Framework. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.


1 Answers

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.

like image 137
SLaks Avatar answered Oct 07 '22 23:10

SLaks