473,651 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic Exception Handling Code

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
Nov 21 '05 #1
3 1628
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?

"dhussong" wrote:
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

Nov 21 '05 #2
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.

"cmay" wrote:
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?

"dhussong" wrote:
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

Nov 21 '05 #3
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.

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2869
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses return code (not generating error/exception) so it is more compatible with other programming language.
7
5989
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2745
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
19
2173
by: KKramsch | last post by:
One of the features from other languages that I miss most in C is trappable exceptions. More specifically, I think it's great to be able to demarcate a whole block of code where several exceptions can happen at various points, so that any one of these exceptions can be trapped and handled by the exception-handling code collected in one place after the block. This also means that the exception generating code can be much cleaner, since...
44
4206
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
5129
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
41
3047
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out three handlers that all do the same thing. So, I could just catch Exception. But, is that...
1
3101
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
4
7835
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have written a generic method which does different things depending on the type of the parameter. I got it to work, but it seems really inelegant. Is there a better way to do this? In the code below, ColumnMap is a simple struct which basically has three properties, Header (a string), Index (an int), TypeOfData (which is a DataType which is a local eNum). _Mapping is a local List<ColumnMap>. public ColumnMap GetMap<T>(T value)
0
8349
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.