Solved

Response.End() in C# throw a ThreadAbortException

Posted on 2003-11-17
6
5,680 Views
Last Modified: 2007-12-19
I used to be able to use the Response.End (JScript in the non-.NET world) without causing any exception.

However, these days I just moved to .NET and I am using C#.  According to some MS document, the Response.End() calls Thread.Abort which in turn causes the ThreadAbortException to appear.  As a result, the following code returns me "Thread was being aborted" exception.

try {
   if (certain condition is not met) {
      Response.Write("xxxx");
      Response.End();
   }
   ...
   ...
}
catch (Exception f) {
   Response.Write(f.Message);
}

Is there a way for me to use Response.End() to end the execution of the current page and also include this Response.End() within a try/catch block without causing an exception to occur.  

My main goal is to terminate execution of the page without causing an exception.

0
Comment
Question by:jsmui
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
msdixon earned 50 total points
ID: 9767161
don't use Response.End()... if your logic in the page is correct, you won't need it. it's a bad habit to get into.

also, get away from Response.Write(), instead, set the text property of a label, or something similar.

think OO, not script
0
 
LVL 5

Expert Comment

by:mondayblueboy
ID: 9768518
Keep using Response.End() by catch the error :o)

try {
   if (certain condition is not met) {
      Response.Write("xxxx");
      Response.End();
   }
   ...
   ...
}
catch (System.Threading.ThreadAbortException e) {
   // let this blank
}
catch (Exception f) {
   Response.Write(f.Message);
}

Hope this helps
0
 
LVL 9

Expert Comment

by:msdixon
ID: 9770596
isn't that a little redundant? if you get an exception, handle it. make sure your methods are not executing anything else after an exception is caught.

Response.End is a bad habit, and not a very good practice.
0
 

Expert Comment

by:onura
ID: 9771145
Hi there,

Generally speaking, you may catch the exceptions that you threw (or couldn't catch) in the Application_Error method of global.asax, and that would be a very nice idea to use that not to show the end user meaningless error message.

Also in your case, you may detect whether the exception is of type ThreadAbortException in this method and act accordingly.

Hope this helps,

Onur
www.onura.net


0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question