[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Response.End() in C# throw a ThreadAbortException

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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