Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception.Message V Exception.InnerException.Message

I am currently logging errors and would like to get the most descriptive details possible. I know I can catch many different types of exceptions but what is the difference between Exception.Message and Exception.InnerException.Message?

like image 551
FlipTop Avatar asked Mar 18 '14 11:03

FlipTop


1 Answers

A program can catch an exception and re-raise a different exception, passing the original caught exception as the InnerException. The Exception(String, Exception) constructor does this for example. This happens in the .NET Framework itself, TypeInitializationException, TypeLoadException, TargetInvocationException, etc are raised this way.

The inner exception is completely unrelated to the raised exception and it is very, very important that you log the inner exception as well to have any hope of diagnosing the root cause of the problem.

The simplest way to do this is to use the ToString() method on the exception object. Which provides the exception message, the stack trace and iterates through the inner exceptions. Everything you need.

like image 93
Hans Passant Avatar answered Oct 09 '22 08:10

Hans Passant