header1.html

Sunday, 21 September 2014

'Thread was being aborted' –Exception for Response.Redirect() method inside try-catch block

If you are monitoring errors of your application you might have noticed that in your error log file lots of 'Thread was being aborted' exception logged.
This Exception occurs when you use Response.Redirect inside of a Try / Catch block. Your code will work and the browser will be redirected but that is reason of getting lots of alerts.
If you are passing QueryString then your application may not work properly because of this issue, so it is important to handle or avoid this kind of exceptions.
There are different ways to resolve this issue,

  • Simplest way to move the Response.Redirect outside the Try/Catch block. 
  • Catch Exception and do nothing, 
                 try 
                    { 
                        if (Condition) 
                          Response.Redirect("newpage.aspx”); 
                    } 
                catch (ThreadAbortException ex) 
                 { // do nothing } 
                 catch (Exception e) 
                  { //Write your Error log logic } 
  • Use overload of Redirect method like, Pass false as second parameter to this overload. Response.Redirect("newpage.aspx”,false); 
I prefer to use 3rd approach as it is easiest one to implement and it works fine in case if you are using QueryString in your application.

Hope this helps you someway. Thank you!!!