Posted: 7/27/2011
Hello,
Im trying to end a Response but on doing so getting this error Thread was being aborted.
Below is my code.
try { string searchText = ""; if (Request.QueryString["q"] != null) { searchText = Request.QueryString["q"]; } var users= GetUsers(searchText); if (users!= null) Response.Write(users); Response.End(); } catch (Exception ex) { objLogger.WriteLogger(CommonConfiguration.GetChannelId(), ex.Message, "Page_Load", "AutoComplete", "ERR", ""); } I'm unable to understand why Im getting this error. If I comment Response.End(). That error doesn't come any more.
try { string searchText = ""; if (Request.QueryString["q"] != null) { searchText = Request.QueryString["q"]; } var users= GetUsers(searchText); if (users!= null) Response.Write(users); Response.End(); } catch (Exception ex) { objLogger.WriteLogger(CommonConfiguration.GetChannelId(), ex.Message, "Page_Load", "AutoComplete", "ERR", ""); }
Can any help me out. Why im getting this error?
Thanks in advance
Sumit Arora
Hi,You should note one thing that methods like Response.Redirect or Response.End will throw ThreadAbortException to end the current processing of page. This is a kind of special exception. From the same link: ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.You can ignore this exception and if you want not to log this, you can go like this:
//Response.End() will throw a ThreadAbortException try { Response.End(); } catch (System.Threading.ThreadAbortException ex) { //You can ignore this } catch (Exception ex) { //Log your other exceptions }
Hi Sumit,
You can also check my blog post I have written sometime ago:
http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx
It might be helpful.
Regards,Hajan