Retrieves a reference to an inner (that may be nested) exception.
[Visual Basic] Overridable Public ReadOnly Property InnerException As Exception [C#] public Exception InnerException {virtual get;} [C++] public: __property virtual Exception* get_InnerException(); [JScript] public function get InnerException() : Exception;
Read-only.
The InnerException property is a reference to an inner exception. This property is set upon initialization of the exception object.
When handling exceptions, it is sometimes desirable to throw another exception that is more indicative of the error that has occurred. In order that the original exception and the information that it carries is not lost, exceptions have a field that can hold a reference to another exception
The InnerException property can be used to create and preserve a series of exceptions during exception handling. It can be very helpful for the developer to create a new exception that catches the exception(s) thrown by prior exception(s). The original exception can be captured by the second exception in the InnerException property, providing the code that handles the second exception with additional information that can be used to handle the error appropriately.
For example, suppose you have a function that reads a file and formats the data that it reads. The code tries to read from the file, but a FileException is thown. The function catches the FileException and throws a BadFormatException. In this case, the FileException could be saved in the InnerException property of the BadFormatException, enabling the code that catches the BadFormatException to examine the InnerException to discover what caused the initial error.