Generic Exception Handling Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to implement a generic exception handling routine that will write
information to a text file at the time the exception occurred. I am using the
Microsoft Application Block for Exception Handling to write to a text file.
This is working great plus I get the call stack which is an added bonus.

What I really want to get is the value of parameters and variables in each
method in the call stack at the time of the exception. I know I can write
code to build a string of all of the current variables and their values and
pass that string to a custom exception handler but this isn't elegant nor
generic. The problem with this is that the code must always be updated as the
method is changed and variables are added or removed. We've done this in some
VB6 code and it's not easy to maintain.

I've found that using Reflection I can get to the CallStack and the
parameters of each method in the call stack but I am not able to get to the
values of those parameters. I am also not able to get to any declared
variables in those methods.

I find it hard to believe that no one else is interested in this type of
exception information so if anyone has any ideas on how to do this or if
anyone has seen or written great exception handling code, please let me know.

Thanks,
Dan
 
Bump...

I am looking for info on this as well.

It seems like one should at least be able to access variables that are
stored at the top of the stack.

In case there is any confusion, what I am looking for is something like this:

public sub MySub
dim x as integer
dim y as integer
dim z as integer

x=5
y=10
z=0

try
'*** thrown an exception
y = x / z
Catch ex as Exception
'*** in here I would like to be able to find out the values of
'*** all local (locals or autos in the VS ide) variables, so
'*** x,y,and z.
end try

end sub


I know that one can write debugging code for every method that logs values,
so for example above you could say:
dim sDebug as string = "X=" & x & ", Y=" & y & ", and z=" & z

but I would like to just iterate through a collection of some kind.


Or, even doing a dump of the stack (variables on the stack, not the calling
tree) would be fine.


I'm guessing that this can not be done, because I would think a lot of
people would be doing it if you could.

So, that begs the question, why can't this be done?
 
It sounds like we want to do the exact same thing. I sent this as an issue to
Microsoft as well as posting it here and the response I received is that it
cannot be done. The responder talked about some tool called SOS but I believe
it can only help you find the value of variables at the time of a crash when
in development mode. So basically you'd need to be able to recreate the error
exactly as it occurred which of course is not usually possible.

At this point I've given up on finding a generic way to do this with some
sort of collection and I've done as you've suggested and built a string to
write out to my error log.

I did submit this to Microsoft as an enhancement request for future versions
of .NET but I'm not hopeful of getting anything soon.
 
There is an article on MSDN this month about SOS. It looked pretty
good until the part where it says that SOS's "minidumps" are 300MB to
1GB for ASP.NET applications... thats crazy.

I totally understand that some people might need a ton of info from a
dump like that, but what I want is basically the .ToString of all
variables in the method that throws the exception, something MUCH
smaller.

This seems like it would be such a great feature, that it makes me
think that there must be some fundamental reason why it isn't available.
 

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

Back
Top