Get access to Parameter values off the Stack

  • Thread starter Thread starter Steve Amey
  • Start date Start date
S

Steve Amey

Hi all

I would like to get the values of Parameters passed to a method from a
centralized exception handling routine. At the moment, the exception is
passed to the handler, and I can get the stack and find out which methods
are in it and what parameters those methods take, but I would really like to
know the values of the parameters, something like below:

HandleException(Exception Ex = MyCustomException)
FillFromDataBase(Integer pkValue = 1)
Main()

If I have a look in the VS.NET designer when debugging, I can see the values
of the parameters. It would be really useful, for logging purposes, to know
the values of the parameters in the exception handler rather than putting
code in each method.

I've looked around for this and saw some-one post a response to a similar
question saying that this is not possible unless you use the Debugging API,
is this true? If it is true, does anyone know of any resources or have any
code samples of how it's done? I can't believe that I'm the first person who
wants to do this, so if it can be done then I'm sure some-one has done it
already!!

Thank you.

Kind Regards,
Steve.
 
I don't know of a way to do it automatically, but you could get the same
effect with slightly more work. Just change your HandleException
method to take the parameter values as a params array (sorry, I do not
know the VB.NET equivalent off hand, but I'm pretty sure it exists - if
not, just use a regular object array):


private void HandleException(Exception e, params object[] parameterValues)
{
System.Diagnostics.StackFrame frame = new
System.Diagnostics.StackFrame(1);
MethodBase method = frame.GetMethod();
ParameterInfo[] parameters = frame.GetMethod().GetParameters();
bool showValues = parameters.Length == parameterValues.Length;
Console.WriteLine(method.Name);
Console.WriteLine("Parameters:");
for (int p = 0; p < parameters.Length; ++p)
{
Console.Write("\t" + parameters[p].Name);
if (showValues)
{
Console.Write("\t" + parameterValues[p]);
}
Console.WriteLine();
}
}


Then any time you call HandleException, just add the method parameters
to the end:

public int DivideNumbers(int x, int y)
{
int answer = 0;
try
{
answer = x / y;
}
catch(Exception e){
HandleException(e, x, y);
}
return answer;
}

Not a perfect solution, but it works.


Joshua Flanagan
http://flimflan.com/blog
 
Hi Joshua

Thank you for your reply.

I was hoping to get what I want done without having to add code in each of
my methods, but I guess I'm not going to have much choice! Also, my
HandleException call is made in the 'top-level' methods, if I receive an
Exception in nested methods I just make the Throw (Ex) call to pass it back
up the chain, so there would be no way to pass through the values of any
Parameters in that method without changing the way I handle exceptions :o(

I guess I'll just wait a bit to see if anyone else comes up with a solution.
I haven't moved to 2.0 yet, we're doing that in Feb 2006, maybe there's
something new that can help me. If not then my parameter values will be lost
forever.....

Kind Regards,
Steve.
 
Back
Top