473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Determine if an exception has been thrown

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
7 8527
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
4
by: Prehaut Anselme | last post by:
Hi, I need to create a WebRequest with an automatic proxy I have the specification of this porxy (host & port) I ask the user their own login and password, but the proxy tells me that the...
4
by: Aren Cambre | last post by:
Why does SmtpMail.Send throw an exception if the MailMessage's BodyFormat = MailFormat.Html? I've searched all over the place and cannot find a solution anywhere. I am running this on Windows XP...
19
by: Diego F. | last post by:
I think I'll never come across that error. It happens when running code from a DLL that tries to write to disk. I added permissions in the project folder, the wwwroot and in IIS to NETWORK_SERVICE...
4
by: Stan | last post by:
When a webservice is called through BeginInvoke asynchrously and an exception is thrown, this exception is not propagated to the client (obviously). Asynch client simply does not care about it. ...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
2
by: Monty | last post by:
I have a GridView in an update panel, and the GridView has an ObjectDataSource. Under certain conditions the Object that is the ObjectDataSource will throw an exception with a specific error...
11
by: Peter Jansson | last post by:
Dear newsgroup, In the following code, it looks as though the exception thrown in the constructor is not caught properly. I get this output: ---- standard output ---- Base() constructor....
0
by: Ralf Jansen | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.