473,396 Members | 1,785 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,396 software developers and data experts.

Current Exception

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
14 1744
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
"=?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
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
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
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
"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
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
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
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
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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: moondaddy | last post by:
I need to set a variable to a session variable (if that's what you call it) like this: dim ds as dataset = HttpContext.Current.Session("CustDataSet") But I get an exception if this variable...
11
by: TheBurgerMan | last post by:
Hi all. I am using W2K3, .NET2 on a machine running AD and Exchange. I started getting the message below last week. I googled the error and not much was returned, but I did find this;...
6
by: Tor Inge Rislaa | last post by:
Finding current Procedure I want to write errors to a log-file, where the error log contain the description of where the error occurred and what kind of error it was. Private Sub...
3
by: namewitheldbyrequest | last post by:
"The XML element 'EnableTheming' from namespace 'http://tempuri.org/' is already present in the current scope" I created a Web Service: I imported System.Data.SqlClient so I could access SQL...
1
by: Jess Chadwick | last post by:
I am attempting to use the Enterprise Library (Jan 2006) Cryptography block to encrypt a credit card number in my ASP.NET 2.0 Commerce Server application. Everything is configured correctly, as...
7
by: fniles | last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection). I read using DataAdapter and DataSet, not DataReader. When many people try to access the database at the same time, I get...
8
by: =?Utf-8?B?R2VvcmdlQXRraW5z?= | last post by:
Greetings! I wrote a small Exe that simply runs Shell to load PowerPoint and launch a particular file, depending on the day of the week. However, it was set up for office 2003 (I naively hardcoded...
1
by: vaibhavchavan | last post by:
Hi I am doing application for signing of soap message with digital certificate . I am using WSE 2.0 I worte the the simple webservice & deployed it on in our server machine . To make it...
3
by: Brett R. Wesoloski | last post by:
I am having problems using HttpContext.Current.Request.Url.Host. I have some code that does this... if (HttpContext.Current != null) { subdomain = HttpContext.Current.Request.Url.Host; }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.