How to obtain detailed info in case of an exception

  • Thread starter Thread starter Helmut Giese
  • Start date Start date
H

Helmut Giese

Hello out there,
in case of an exception how can I extract the reason or other
information in order to present it to the user?
Specifically: If an exception occurs when deserializing an XML file,
VS tells me line and column number which is of course a big help.

Any help or link will be greatly appreciated.
Best regards
Helmut Giese
 
in case of an exception how can I extract the reason or other
information in order to present it to the user?
Specifically: If an exception occurs when deserializing an XML file,
VS tells me line and column number which is of course a big help.

You have an instance of a class in the Exception hierarchy.

Whatever is available is in that.

First check if the ToString method gives you what you need.

The check whether the specific class has some extra
properties/methods.

Arne
 
You have an instance of a class in the Exception hierarchy.

Whatever is available is in that.

First check if the ToString method gives you what you need.

The check whether the specific class has some extra
properties/methods.

Arne

Also check the InnerException property. It may be null, but if not,
then that is usually where the problem resides.
 
Helmut said:
Hello out there,
in case of an exception how can I extract the reason or other
information in order to present it to the user?
Specifically: If an exception occurs when deserializing an XML file,
VS tells me line and column number which is of course a big help.

Note that the file/line/column is of little use to the user. :)

I have found when reporting exceptions that two pieces of information
are most useful to the user: a description of what _my_ code was trying
to do when the exception occurred, and the string retrieved from the
Message property of the exception instance that was caught.

The user generally cannot do anything useful with the stack trace, code
locations, etc. though I suppose if you are likely to get good feedback
from the users, having a button on an error alert via which they can get
those details to forward to you might be worthwhile.

As Mike says, it also may be of use to report the inner-most exception
if not the same as the one your code caught, as that's the one that
would apply most specifically to whatever actually went wrong.

Pete
 
Arne said:
You have an instance of a class in the Exception hierarchy.

Whatever is available is in that.

First check if the ToString method gives you what you need.

In theory you can also check recursively for InnerExceptions, though
whether there are going to be any depends on where the exception is
coming from.
 
Harlan said:
In theory you can also check recursively for InnerExceptions, though
whether there are going to be any depends on where the exception is
coming from.

Technically, you can check iteratively. You could also check
recursively, but it's not necessary. :)

Standard list traversal:

Exception e;

while (e.InnerException != null)
{
e = e.InnerException;
}

Pete
 
Peter said:
Technically, you can check iteratively. You could also check
recursively, but it's not necessary. :)

Standard list traversal:

Exception e;

while (e.InnerException != null)
{
e = e.InnerException;
}

Um ... quite right. :-)
 
Thanks to all who responded: Pointing me to the inner exception helped
a lot.
Best regards
Helmut Giese
 
Back
Top