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

Current Exception

P: n/a
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/
Nov 17 '05 #1
Share this Question
Share on Google+
14 Replies


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

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"Chad Z. Hower aka Kudzu" wrote:
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/

Nov 17 '05 #2

P: n/a
"=?Utf-8?B?U2FsdmFkb3I=?=" <Sa******@discussions.microsoft.com> wrote in
news:5F**********************************@microsof t.com:
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/
Nov 17 '05 #3

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

---
Adam Clauss

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn**************************@127.0.0.1...
"=?Utf-8?B?U2FsdmFkb3I=?=" <Sa******@discussions.microsoft.com> wrote in
news:5F**********************************@microsof t.com:
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/

Nov 17 '05 #4

P: n/a
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

P: n/a
Adam Clauss <ca*****@tamu.edu> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
"Adam Clauss" <ca*****@tamu.edu> wrote in
news:11*************@corp.supernews.com:
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/
Nov 17 '05 #7

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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/
Nov 17 '05 #8

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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/
Nov 17 '05 #9

P: n/a
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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. :)


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP***********************@msnews.microsoft.co m:
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/
Nov 17 '05 #11

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

---
Adam Clauss
"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn**************************@127.0.0.1...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP***********************@msnews.microsoft.co m:
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/

Nov 17 '05 #12

P: n/a
"Adam Clauss" <ca*****@tamu.edu> wrote in
news:11*************@corp.supernews.com:
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/
Nov 17 '05 #13

P: n/a
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/...24/247002.aspx
"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn**************************@127.0.0.1...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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/

Nov 17 '05 #14

P: n/a
"J.Marsch" <jm*****@newsgroup.nospam> wrote in
news:OD**************@TK2MSFTNGP09.phx.gbl:
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/...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/
Nov 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.