By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

Determine if an exception has been thrown

P: n/a
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

--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com
Oct 17 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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;
}

"Ralf Jansen" wrote:
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

--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com
Oct 17 '08 #2

P: n/a
On Fri, 17 Oct 2008 13:49:58 -0700, Ralf Jansen
<ra****************@deepinvent.comwrote:
[...]
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?
I don't know the answer to that specific question. There's probably a
way, though it could be very messy. But it seems to me that you could
just track the state logically:

bool fException = true;

try
{
...

// very last thing in the try block:
fException = false;
}
finally
{
if (fException)
{
// an exception was thrown
}
}

That's the most literal-yet-simple solution to your question I see. That
said, what's wrong with something like this:

try
{
}
catch
{
// log that an exception occurred

throw;
}
finally
{
// do whatever
}

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
Oct 17 '08 #3

P: n/a
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?

KH schrieb:
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;
}

"Ralf Jansen" wrote:
>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

--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com

--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com
Oct 17 '08 #4

P: n/a
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.
--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com
Oct 17 '08 #5

P: n/a
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.
"Ralf Jansen" wrote:
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?

KH schrieb:
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;
}

"Ralf Jansen" wrote:
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

--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com


--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com
Oct 17 '08 #6

P: n/a
On Fri, 17 Oct 2008 14:21:52 -0700, Ralf Jansen
<ra****************@deepinvent.comwrote:
[...]
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.
What about this:

using (LoggingService.TraceObject to =
LoggingService.Trace("ActivityID"))
{
// do whatever...

to.ExceptionOccurred = false;
}

TraceObject could be the actual class (or base class) for the object
implementing IDisposable. Or, if for some reason an inheritance isn't
appropriate, it could be a simple one-property interface (call it
IExceptionState or something like that). The ExceptionOccurred property
could be write-only, basically providing input to the object implementing
IDisposable that you got all the way through the code block without an
exception.

Another alternative that doesn't rely on IDisposable:

LoggingService.Trace("ActivityID", delegate
{
// do whatever...
});

Where the LoggingService.Trace() method looks something like this:

void Trace(string strID, Action action)
{
bool fException = true;

try
{
action();

fException = false;
}
finally
{
// do your logging, taking fException into account
}
}

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.

Personally, I'd go with an approach that didn't use IDisposable.

Pete
Oct 18 '08 #7

P: n/a
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

--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Central Email Archiving The Easy Way - http://www.mailstore.com
Oct 18 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.