?
Solved

Response.End() in C# throw a ThreadAbortException

Posted on 2003-11-17
6
Medium Priority
?
5,740 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 150 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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