tags:

views:

981

answers:

2

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
  // ...

}
+6  A: 

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.

SLaks
So how could i get it to ignore that exception and continue executing the code in block B?
Aros
Are you sure you want to?If someFunctionCall is redirecting or ending the response, you probably shouldn't continue it
SLaks
What does someFunctionCall do?
SLaks
It makes a XML-RPC call to 3rd party vendor. No response end or redirct. If it is an older version of the XML-RPC API I would like to ignore it and continue with default values.
Aros
Call Thread.ResetAbort in the catch block. Also, ask the vendor what the exception is from. What's the exception's stack trace?
SLaks
Thanks SLaks, appreciate the help. I will try that.
Aros
That way is not working.
Braveyard
@Aaron: What are you talking about?
SLaks
A: 

Try this way :

This is my extension method :

 public static void WriteJSONObject(this HttpResponse response, object content) {
            response.ContentType = "application/json";
            response.Write(new JavaScriptSerializer().Serialize(content));
            response.End();

 }

And the logic :

public void RegisterUser() {
    try {
        Response.WriteJSONObject(new { Result = "See hello" });
    } 
    catch (Exception err) {
        if (err.Message != "Thread was being aborted.")
            Response.WriteJSONObject(new { Result = err.Message });
        else {
            Response.End();
        }
    }
}
Braveyard
This is wrong. You should _never_ compare an exception's `Message` to a hard-coded string. Instead, you can write `catch (ThreadAbortException) {} catch (Exception ex) { ... }`. Also, there's no point in calling `Response.End` if there's already a `ThreadAbortException`.
SLaks
It is working just fine even it looks wrong.
Braveyard
P.s : The solutions above didn't work for me like most people. No matter what I did didn't work so this is the only solution I've found and works just fine.
Braveyard
I wasn't saying that it won't work; I'm saying that it's a horrible idea. Your code is solving a different problem that has nothing to do with this question.
SLaks
I think you may be right :)
Braveyard