e.InnerException.Message

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

I am trying to understand the uses for the the InnerException method of an
Exception object.

Looking at the documentation, I have summized this about using the
InnerException method....
If you have a method which can cause and exception, but you really do not
want to deal with the exception in that method, but you need/want to
provide some "details" of the circumstances of the error, is this when you
would catch the exception in that method, provide the details, and then
generate another exception passing along those details? Then the exception
woul go up the chain until some procedure caught the exception and then
would deal with it all the while have all the appropriate infomation to
deal with it?

Is this accurate?

What is not particular clear to me is how I would add value to this.
Suppose I wanted the "error message text" to be something like "Procedure
failed in .... The reason was.....". In order to do this I would have to
create an instance of my own error object and set the message accordingly?




System.Exception(e.InnerException.Message, e.InnerException)

Thanks In Advance for your assistance!!!!!!
 
The advantage of this to me is to be able to pass a 'stack' of errors that
occured all the way up to my user interface and/or tracing class.

If you have a class that accesses a database and throws an error, you could
catch it, and then throw an exception that you create adding the caught
exception as the InnerException paramter. You could again catch that error
from the calling procedure and throw another error with the previous error
as it's InnerExcpeption paramter.

Once you are at a point where you display or record the error, you can use a
recursive method to record the error, check to see if there is an
InnerException, and if so call the same method to record that error and
check for another until you've reached the original error.

I hope I didn't make that more confusing than it is! :-)

-Darrin
 
Jim Heavey said:
I am trying to understand the uses for the the InnerException method of an
Exception object.

You may have a couple reasons to use this. In your application you maybe
have designed it so that you always return "your" exception to all of your
consumers. You may have extra info that you include with all of "your"
exceptions. The InnerException is a place where you can store the original
exception that occurred. Second, you may have some kind of interpretation
of what happened and would like to return that interpretation to the
consumer, rather than the sometimes cryptic message that may come from the
deeper routine. For example, an Invalid Operation exception may be thrown
from some routine, but you know it may be just because your out of disk
space (or somesuch). You could provide the user friendly interpretation,
while still including the original exception info in the InnerException.
What is not particular clear to me is how I would add value to this.
Suppose I wanted the "error message text" to be something like "Procedure
failed in .... The reason was.....". In order to do this I would have to
create an instance of my own error object and set the message accordingly?

Derive your own exception class from System.Exception, or whichever FCL
exception class is most appropriate for you.

-- Alan
 
Back
Top