Why is InnerException used

T

Tony Johansson

Hi!

Some exception has further information in the Inner Exception property.
For example the SmtpException has useful information in the InnerException
that can be used
Here is an example
catch (SmtpException ex)
{
Exception inner = ex.InnerException;
MessageBox.Show("Could not send message: " + inner.Message,
"Problem sending message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

So my question is why is not the InnerException.Message information put into
the SmtpException.Message ?

//Tony
 
W

Willem van Rumpt

Tony said:
Hi!

Some exception has further information in the Inner Exception property.
For example the SmtpException has useful information in the InnerException
that can be used
Here is an example
catch (SmtpException ex)
{
Exception inner = ex.InnerException;
MessageBox.Show("Could not send message: " + inner.Message,
"Problem sending message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

So my question is why is not the InnerException.Message information put into
the SmtpException.Message ?

//Tony

What use would it be to copy and repeat the same message in a new
exception? The wrapping exception can (potentially) be more clear and
provide additional information than the exception thrown from some deep
ly nested library call.
 
H

Harlan Messinger

Tony said:
Hi!

Some exception has further information in the Inner Exception property.
For example the SmtpException has useful information in the InnerException
that can be used
Here is an example
catch (SmtpException ex)
{
Exception inner = ex.InnerException;
MessageBox.Show("Could not send message: " + inner.Message,
"Problem sending message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

So my question is why is not the InnerException.Message information put into
the SmtpException.Message ?

An Exception isn't just a message. It has a type, and it has a a
HelpLink, a Source, a StackTrace, and a TargetSite. It may even have its
own InnerException. All of these may be useful to a calling method or to
the user or developer.
 
G

Göran Andersson

Hi!

Some exception has further information in the Inner Exception property.
For example the SmtpException has useful information in the InnerException
that can be used
Here is an example
catch (SmtpException ex)
{
Exception inner = ex.InnerException;
MessageBox.Show("Could not send message: " + inner.Message,
"Problem sending message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

So my question is why is not the InnerException.Message information put into
the SmtpException.Message ?

//Tony

An exception is usually put as inner exception if it contains
information that needs to be retained and can't be combined into a new
exception. An exception can contain a lot more than just the message,
like a stack trace.

A clear example is the SoapException which is thrown when a web service
call is answered with a SOAP error message. If the web service is a .NET
service, the error message contains a serialised excpetion object from
the server. This is deserialised and put in the InnerException of the
SoapException object. As the exceptions are created on different
servers, they should clearly remain separate instead of trying to be
combine them.
 
P

Patrice

So my question is why is not the InnerException.Message information put
into the SmtpException.Message ?

Common best practice. It's best to provide separated information that can be
gathered easily as needed rather than to pack all in a single item making
hard to extract a particular info out of it.

Not sure what you want to get in your case, but you could perhaps try
ex.ToString()...
 
A

Arne Vajhøj

Some exception has further information in the Inner Exception property.
For example the SmtpException has useful information in the InnerException
that can be used
Here is an example
catch (SmtpException ex)
{
Exception inner = ex.InnerException;
MessageBox.Show("Could not send message: " + inner.Message,
"Problem sending message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

So my question is why is not the InnerException.Message information put into
the SmtpException.Message ?

Using the inner exception text as the entire exception text is not good
because the outer exception may actually need to provide some text
as well.

Concatenating the texts is very messy - start considering how to pick
a separator guaranteed not to be present in the texts.

Inner exceptions are typical used as part of encapsulation.

Arne
 

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

Top