Using Response.Redirect(url) in Try / Catch Block

If my response to errors in a try/catch block is to redirect users to an error page, the try/catch block behaves as if there was an error when there was not. If I change it to do something else, the code works fine.

Example:

try
{
    //do this SQL server stuff
}
catch
{
   Response.Redirect(error.htm)
   //Change this to lblErr.Text = "SQL ERROR"; and the code in try works fine.
}

From another post I learned there was a boolean overload to the Response.Redirect() method. I tried both true and false and the try/catch block still behaved as if there were an error.

What's the deal?

Answers


When you Response.Redirect, that throws a ThreadAbortException. So to get the outcome you are describing you'll want to mod your code as follows:

try  
{
   // Do some cool stuff that might break
}
catch(ThreadAbortException)
{

}
catch(Exception e)
{
  // Catch other exceptions
  Response.Redirect("~/myErrorPage.aspx");
}

Need Your Help

How to convert integers to base64 (0-9A-Za-z)

java string character-encoding compression base64

I have been trying to reduce the length of the way. I represent some integer ID's in my program. For Example

Numbers from database refuse to be added/multiplied

php mysql math types

I am pulling a set of numbers from a MySQL database and am trying to do some simple math on them to calculate a total price to put into an invoice, but PHP isn't cooperating. I thought it might be a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.