Solved

Response.End() in C# throw a ThreadAbortException

Posted on 2003-11-17
6
5,513 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now