Current Exception

  • Thread starter Chad Z. Hower aka Kudzu
  • Start date
C

Chad Z. Hower aka Kudzu

Its a bit tough to explain why I need this, so I wont. Consider it academic for now.

I would like to detect if an exception is currently being thrown, but I cannot use a try statement. This
is not what I want to do, but this will demonstrate what I want to do:

try
throw exception
finally {
Foo();
}


Foo() {
if (An Exception is currently being thrown/handled) {
Do sometihng
}
}

I realize in the above case I coudl use catch, pass it as an argument, and re throw. But for the actaul
situation Im looking at, I cannot do that and will not have access to a catch.

Ive looked at StackTrace, but it does not seem to hold this information either unless Ive missed it on
quick glance. In Delphi there is a "global" that holds the current exception for the current thread. Is
there anything like this anywhere in .NET, or any way to obtain it?


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
G

Guest

Hi,

What you can do is not to handle to exception and then delegate
AppDomain.UnhandledException, this will execute the function that you want
and then it will go back to your code.

Hope this helps
SAlva
 
C

Chad Z. Hower aka Kudzu

=?Utf-8?B?U2FsdmFkb3I=?= said:
What you can do is not to handle to exception and then delegate
AppDomain.UnhandledException, this will execute the function that you
want and then it will go back to your code.

Thanks, but wont work. I cant use an event, I need to determine from inline code.

I cant set a flag either, because its threaded (Which could find some way to track maybe) but the
bigger issue is that I suspect its like Delphi's exception handler, it happens AFTER all the code has
made it out to the outer levels so it would happen after my code executed.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
A

Adam Clauss

Could you create your own "global" variable (a static member of some class)
and set it yourself when the catch the exception (it HAS to be caught
somewhere...)
Then in whatever code you are actually handling it, you can check that
static member (and set it to null when done?)
 
J

Jon Skeet [C# MVP]

Chad Z. Hower aka Kudzu said:
Its a bit tough to explain why I need this, so I wont. Consider it
academic for now.

I would like to detect if an exception is currently being thrown, but
I cannot use a try statement. This is not what I want to do, but this
will demonstrate what I want to do:

try
throw exception
finally {
Foo();
}


Foo() {
if (An Exception is currently being thrown/handled) {
Do sometihng
}
}

I realize in the above case I coudl use catch, pass it as an
argument, and re throw. But for the actaul situation Im looking at, I
cannot do that and will not have access to a catch.

Ive looked at StackTrace, but it does not seem to hold this
information either unless Ive missed it on quick glance. In Delphi
there is a "global" that holds the current exception for the current
thread. Is there anything like this anywhere in .NET, or any way to
obtain it?

I haven't come across any such thing. If you don't need the actual
exception, just need to know whether the finally block is executing due
to an exception or normal completion, I'd suggest using a boolean
variable (eg success) which you only set to true as you come out of the
try block normally.
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
Could you create your own "global" variable (a static member of some class)
and set it yourself when the catch the exception (it HAS to be caught
somewhere...)
Then in whatever code you are actually handling it, you can check that
static member (and set it to null when done?)

That wouldn't help at the finally block level though, because (assuming
I'm understanding Chad right) the finally block is going to execute
before the catch block (as the catch block is higher up the call
stack).
 
C

Chad Z. Hower aka Kudzu

Adam Clauss said:
Could you create your own "global" variable (a static member of some
class) and set it yourself when the catch the exception (it HAS to be
caught somewhere...)
Then in whatever code you are actually handling it, you can check that
static member (and set it to null when done?)

I thought about that. But basically I wont have control over the code where the exception occurs. It
could be another users code, etc, but as part of his try finally, or other my function gets called.
(its actaully more complicated than that, the user doesnt directly even call my function). So I
cannot use try statemetns to hook and catch.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Jon Skeet said:
That wouldn't help at the finally block level though, because
(assuming I'm understanding Chad right) the finally block is going to
execute before the catch block (as the catch block is higher up the
call stack).

Well not quite. Assume my code gets called from a finally, it doesnt but its the closest way to
dsecribe it to you:

try
Throw
finally
CAllMyMethod - Cannot pass any params


Now the problem is - this code that you "see" here I have no control over. Its user code - but my
method does get called. But no data can be passed to my method and I cannot control the user to
have them put in any try structure at all, so assume that my method does get called here - AND the
exception is still thrown, thats when I need to detect it. The problem with the AppDomain event is that
if its like Delphi and other frameworks (I highlys suspect it is) that it gets called AFTER all this
happens. That its it reaches back to the top level and out of all user code, THEN triggers the even.
Which means its after my code has already been called. :)




--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Jon Skeet said:
I haven't come across any such thing. If you don't need the actual
exception, just need to know whether the finally block is executing due

Actually - I dont need it. Forgot to mention that. I just need to know if one is in the process of being
thrown (Active) or not.
to an exception or normal completion, I'd suggest using a boolean
variable (eg success) which you only set to true as you come out of the
try block normally.

But I cant control the try..block - I was just showing this as an example as posting the actualy
example would be a lot more involved.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
J

Jon Skeet [C# MVP]

Chad Z. Hower aka Kudzu said:
Well not quite. Assume my code gets called from a finally, it doesnt
but its the closest way to dsecribe it to you:

try
Throw
finally
CAllMyMethod - Cannot pass any params


Now the problem is - this code that you "see" here I have no control
over. Its user code - but my method does get called. But no data can
be passed to my method and I cannot control the user to have them put
in any try structure at all, so assume that my method does get called
here - AND the exception is still thrown, thats when I need to detect
it. The problem with the AppDomain event is that if its like Delphi
and other frameworks (I highlys suspect it is) that it gets called
AFTER all this happens. That its it reaches back to the top level and
out of all user code, THEN triggers the even. Which means its after
my code has already been called. :)

Right. I think you're in trouble then, I'm afraid :( I don't think
there's any way of detecting whether or not an exception is being
thrown...
 
C

Chad Z. Hower aka Kudzu

Jon Skeet said:
Right. I think you're in trouble then, I'm afraid :( I don't think

Thats what I thought. Fortunately its not a need, but sometihng that if I could have accomplished
would have added some nice functionality to what I am working on.

Its too bad it cant be gotten in the StackTrace class, or as some static in the current thread. (You can in
Delphi. :) )



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
A

Adam Clauss

I'm still a little confused as to how/why you say you do not have control
over Foo.

I mean, obviously Foo is defined somewhere that the user's can call it from
correct? (Or is the user not calling it either?)

Seems to me, just make Foo() have a parameter (just a bool since you said
you dont' need the exception) and require users to pass it whenever they
call your method. Is the user-code already compiled and will never be
changed?

If there is some user code already existing and you do not want to break
backwards compatibility, could you define 2 Foo's? One with a parameter and
one without. The one without makes an assumption about whether or not it is
being called from an exception and calls Foo(true) or Foo(false).

That would enable existing code to still work, while letting new code take
advantage of the added functionality.
 
C

Chad Z. Hower aka Kudzu

Adam Clauss said:
I'm still a little confused as to how/why you say you do not have
control over Foo.

I mean, obviously Foo is defined somewhere that the user's can call it
from correct? (Or is the user not calling it either?)

The user isnt calling it directly, but its getting called. Since the user isnt calling it directly, they cant
pass any info to it.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
J

J.Marsch

Well, yesterday I was going to write that if you could wait until .net 2.0,
you would be in luck. Unfortunately, the feature that I was going to write
about appears to have been dropped from beta 2.

There was supposed to be an ExceptionThrown event on the AppDomain object.
This event was supposed to fire whenever an exception was thrown (handled or
unhandled), but now it's A.W.O.L. Here is a Brad Abrams post that discusses
it: http://blogs.msdn.com/brada/archive/2004/10/24/247002.aspx
 
C

Chad Z. Hower aka Kudzu

J.Marsch said:
Well, yesterday I was going to write that if you could wait until .net
2.0, you would be in luck. Unfortunately, the feature that I was
going to write about appears to have been dropped from beta 2.

Thats a shame. Its not terribly difficult. A member could be added to the Thread class, and
accessed from there.
There was supposed to be an ExceptionThrown event on the AppDomain
object. This event was supposed to fire whenever an exception was
thrown (handled or unhandled), but now it's A.W.O.L. Here is a Brad
Abrams post that discusses it:
http://blogs.msdn.com/brada/archive/2004/10/24/247002.aspx

Hmm, that would be useful too, but would be even better if they just add it to the Thread instance so
anyone can look at it.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 

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