Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way for ASP.NET server to detect an throw exception error?

Status
Not open for further replies.

fletchsod

Programmer
Dec 16, 2002
181
I know about the application_error function in global.asax which is no problem. (
I noticed the throwing of the try-catch exception error does not trigger the application_error (global.asax).

So, is there a way this can be accomplished somehow?
 
try catch does exactly that, catches the exception if you want to bubble up the exception you either need to throw it again, or just not catch.

the following code examples are logically the same but the second is the proper implementation as we are not altering the exception in anyway.
Code:
try
{
   do something
}
catch(SpecificException e)
{
   // clean up code
   throw;
}
Code:
try
{
   do something
}
finally
{
   //clean up code
}
the only time you should catch an exception is
1. you expect it, so you should never/rarely catch Exception. instead you should catch specific types like
FormatException
DivideByZeroException
IOException
2. you can handle the exception. this could mean wrapping the exception in a user friendly exception type.
catch(Exception e)
{
throw new MyUserFriendlyExcpetion("my message", e);
}

it's pretty safe to say you should never swallow an exception. doing so you will loose all information about how to solve the problem.
Code:
try
{
  do something
}
catch(Exception e)
{
   //empty block = bad
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I agree with your comments. I have been giving this some thoughts.

I have an user friendly exception type that redirect the ASP.NET webpage to an error webpage. My boss and some employees don't like the .NET application error description on the website.

I was looking for a way an error email would be send to me, so I can fix the bug in timely manner as the web-users don't alway tell us of an error or are good at describing an error.

Um, I think I can enclosed an email function in both of them (Application Error & error webpage) that would send me an email.

Thanks..
 
what you want is a logging mechanism to send you the error and then display a pretty message to the end user. this is a common scenario.

as jbension suggests, use a logging framework capture the exception. there is also log4net and nlog which are alternatives to MS logging block. these frameworks allow you to log the exception to multiple destinations, file, db, email, msmq, etc.

the way you would use this. is allow exceptions to bubble up to the global error handler. at this point get the exception, log it, and then redirect to the error page.

the code in the global file is
Code:
var e = Server.GetLastError();
logger.Error(e.Message, e);
Response.Redirect("friendlyerror.aspx");

you would not need any try/catch blocks in your code behind.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Right! :)

logging framework look nice. I'll try it out some other time. I'm trying to keep it simple cuz most employees in my company don't know .NET. LOL!

I also found an interesting object that trigger the application level error "throw new ApplicationException('This is an unhandled exception.');" but I won't use it but it is nice to know there is one.
 
Some of them employees are developers and a few are tech supports. ;-)
 
I'm the only person in the building who know .NET
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top