Solved

Response.End() in C# throw a ThreadAbortException

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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