Determine if an exception has been thrown

R

Ralf Jansen

For logging purposes i want to determine if the current executing code is
running in an ~exceptionhandling context~. I need no details about the exception
just if an exception has been thrown and hasn't been handled yet.

Following code should make clear what i mean and where i need that info.

try
{
try
{
throw new Exception();
}
finally
{
// here i want to know if an exception has been thrown
}
}
catch(Exception e)
{
// doSomething
throw;
}


If i put a breakpoint inside the finally block i can inspect the Exception in
the debugger through the '$Exception' pseudo variable. Is it possible to get
that also in code?

Thanks
 
K

KH

try
{
bool ex = false;
try
{
throw new Exception();
}
catch(Exception ex)
{
ex = true;
throw ex;
}
finally
{
// here i want to know if an exception has been thrown
if (ex)
{
// an exception was thrown
}
}
}
catch(Exception e)
{
// doSomething
throw;
}
 
R

Ralf Jansen

Thanks, but in this case the finally Block will be before the Exception Block.
As i said its for a logging purposes or to be more precise it is used for timing
of a codeblock. The actual code is working via a the using statement and looks
more like this

try
{
using(LoggingService.Trace("ActivityID"))
{
DoSomethingTimeConsuming();
}
}
catch(Exception e)
{
// doSomething
throw;
}

My LoggingService.Trace methods return an IDisposable object which does the
timing. It starts timing in its constructor and logs the results in its Dispose
method so i need to know if an exception happened in the Dispose method of that
object.

More ideas?
 
R

Ralf Jansen

In other words, if you care that an exception was thrown, then just
catch it, deal with it, and continue. Why make things complicated?

Pete

see my reply to KH.

The complicted thing is that im trying to find a tight syntax for the logging,
exception handling stuff that will not distract from the actual purpose of the
code.
 
K

KH

Thanks, but in this case the finally Block will be before the Exception
Block.

Helps if you post the actual problem you need help with. You know the
"Exception block" (catch block?) can't come after the finally right?

Like Peter said it sounds like you're making it way more complicated than it
needs to be. using statement expands to a try/finally anyways - 'using' isn't
any more efficient than writing it out yourself.
 
R

Ralf Jansen

Ultimately, you're using IDisposable for something other than the
purpose for which it was intended. If you insist on using IDisposable
for this purpose, you may have to accept that this use isn't going to
fit perfectly into the code, unless you are willing for your logging
object to have some complicated, ugly hacks in it.

Can't disagree here. Actually i found a solution in using
Marshal.GetExceptionPointers() but as you said its a hack. And presumably it
wouldn't be the last one needed if i go further down that road.

I like the delegate idea and will see if it fits .... on Monday ;)

Thanks Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top