I haven't found anything in the reflection library that enables you to
detect such a thing.
You can, using stack frames, retreive from Method2 which method is the
caller like this:
StackTrace st = new StackTrace(2, false);
StackFrame sf = st.GetFrame(0);
MethodBase method = sf.GetMethod();
But neither StackFrame nor MethodBase gives any direct information on the
calling context.
You could perhaps analyze the IL code or source code somehow using the call
positions found in StackFrame.
It is probably a LOT easier to have two overloads of Method2, one that
accepts an exception object as parameter and one without any parameters.
Peace,
Peter Lillevold
-----Original Message-----
From: Jan Strube [mailto:[EMAIL PROTECTED]]
Sent: 31. mai 2002 02:31
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Detect Exception Handler
I am wondering if it is possible to detect if some code executes as an
Exception handler. What I am after is the implementation of
IsExecutingAsExceptionHandler() in the following sample:
void Method1()
{
Method2();
try
{
throw new Exception();
}
catch
{
Method2();
}
}
void Method2()
{
if (IsExecutingAsExceptionHandler())
MessageBox.Show("Executes as exception handler");
else
MessageBox.Show("Normal execution");
}
I am aware that I could pass on the exception object to Method2, but I am
looking for a way to avoid this.
Thanks,
Jan Strube
You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or subscribe to other DevelopMentor lists at
http://discuss.develop.com.
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.