PC Review


Reply
Thread Tools Rate Thread

Determine if an exception has been thrown

 
 
Ralf Jansen
Guest
Posts: n/a
 
      17th Oct 2008
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


 
Reply With Quote
 
 
 
 
KH
Guest
Posts: n/a
 
      17th Oct 2008
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
>
>
>

 
Reply With Quote
 
Ralf Jansen
Guest
Posts: n/a
 
      17th Oct 2008
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


 
Reply With Quote
 
Ralf Jansen
Guest
Posts: n/a
 
      17th Oct 2008

> 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


 
Reply With Quote
 
KH
Guest
Posts: n/a
 
      17th Oct 2008
> 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
>
>
>

 
Reply With Quote
 
Ralf Jansen
Guest
Posts: n/a
 
      18th Oct 2008
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine if an exception has been thrown Ralf Jansen Microsoft C# .NET 0 17th Oct 2008 09:47 PM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs Microsoft ASP .NET 1 16th Nov 2007 05:57 PM
Exception has been thrown by the target of invocation exception =?Utf-8?B?VmFs?= Microsoft ASP .NET 0 8th Jun 2005 06:11 PM
Exception of type System.Exception was thrown Selen Microsoft ASP .NET 0 28th May 2004 08:19 AM
Exception of type System.Exception was thrown. Selen Microsoft C# .NET 6 28th May 2004 02:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:49 PM.