Exception OnThrown ?

  • Thread starter Thread starter Tomas Tichy
  • Start date Start date
T

Tomas Tichy

Hi,

In my errorhandling framework i would like to log error message after
exception is thrown. I do not want the programmer to do it explicitely, so i
would like to incorporate it into Exception derived class. I'm looking for
some sort of OnThrown override. I know the .NET doesnt provide anything like
this, but would it be possible to implement lets say using managed c++ ?

If not, what would be other suggested approach to this problem ?

Thanx for any hint.

T.
 
Hey Thomas

I am not sure I understand what it is that you want. But as long as the thrown exceptions are not caught anywhere, you can hook into the AppDomain.UnhandledException event to be notified of these uncaught exceptions. Is this what you want

Regards, Jakob.
 
If you know in advance what type you want to throw and it is always the
same, then the easiest way to log it is to add the logging code in the
constructor of the exception. The code that throws it could look like
this...
if ( somethingBadHappened )
throw MyException("Describe the problem here.");

If these are for exceptions thrown by your own code but for a number of
different types, you can easily write a centralized routine that logs the
exception, creates an exception object of the appropriate type and returns
it to the caller, so that you could write code like this.

if ( somethingBadHappened )
throw CreateAnExceptionAndLogIt( "Something bad happened.",
typeof(AnExceptionType) );

Your method CreateAnExceptionAndLogIt returns a System.Exception type but it
actually constructs an exception of the type you passed in. It could log
whatever data you considered important.

If you are trying to log exceptions thrown by other components, such as
mscorlib, then you can only do so in the catch block where they are caught
or in the unhandled exception handler if they are not caught.
 
Currently I'm using same approach as you sugested, but it is flawed in its
design:

At the time of construction of the exception (and logging) i do not know
stack trace and target site - these properties are (obviously) populated at
the 'throw' time.

T.
 
Something like AppDomain.ThrownException is what I'm looking for. I need to
log *EVERY* level, not only unhandled exceptions. On each level I'm
associating caught exception wiht some context bussines object, so that i
can track back lets say errors raised during processing of given invoice. I
don't think i will find elegant solution to this problem, i'm just
frustrated about :

[C#]
catch ( Exception ex )
{
ExceptionManager.Publish( ex ); // log4net.Error( ex ) ;
throw ex;
}

T.


Jakob Christensen said:
Hey Thomas,

I am not sure I understand what it is that you want. But as long as the
thrown exceptions are not caught anywhere, you can hook into the
AppDomain.UnhandledException event to be notified of these uncaught
exceptions. Is this what you want?
 
Tomas,
In addition to the others comments.
[C#]
catch ( Exception ex )
{
ExceptionManager.Publish( ex ); // log4net.Error( ex ) ;
throw ex;
}
Did you know that "throw ex" will reset the stack trace property of the
exception, that simply using "throw" will preserve the original stack trace
of the exception? (when used in a catch block).


Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/default.aspx

Unfortunately the article is not on-line yet.

Hope this helps
Jay



Tomas Tichy said:
Something like AppDomain.ThrownException is what I'm looking for. I need to
log *EVERY* level, not only unhandled exceptions. On each level I'm
associating caught exception wiht some context bussines object, so that i
can track back lets say errors raised during processing of given invoice. I
don't think i will find elegant solution to this problem, i'm just
frustrated about :

[C#]
catch ( Exception ex )
{
ExceptionManager.Publish( ex ); // log4net.Error( ex ) ;
throw ex;
}

T.


Jakob Christensen said:
Hey Thomas,

I am not sure I understand what it is that you want. But as long as the
thrown exceptions are not caught anywhere, you can hook into the
AppDomain.UnhandledException event to be notified of these uncaught
exceptions. Is this what you want?
Regards, Jakob.
 
Tomas Tichy said:
Currently I'm using same approach as you sugested, but it is flawed in its
design:

At the time of construction of the exception (and logging) i do not know
stack trace and target site - these properties are (obviously) populated at
the 'throw' time.
You don't need an exception object to get a stack trace, you can get a stack
trace directly from the StackTrace class. Granted, it's more overhead to
get the stacktrace, callsite, etc. yourself, but since exceptions by their
very nature are non-performant, the extra overhead should be negligible by
comparision.

The exception object is populated with the stack trace information when the
call to

throw ex;

is made, where ex is the exception object. If you just call

throw; // in a catch handler,

this does not populate the exception object with stack trace information, it
reuses the stack trace in the existing exception object (i.e. rethrows it).

Dave
 
Back
Top