Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is inner Exception

I have read the MSDN but, I could not understand this concept.

Correct me if I am wrong,

A innerexception will be used in hand with current exception.

Inner exception will occur first and then the current exception will occur (if there is an exception) that is the reason why InnerException is checked against null. In order to retain inner exception we have to pass it as a parameter.

Am I right with this?

like image 587
user2526236 Avatar asked Apr 03 '14 01:04

user2526236


People also ask

What is inner exception give example?

Exception. An object that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null if the inner exception value was not supplied to the constructor. This property is read-only.

How do I find inner exception details?

When you're debugging and you get an exception, always, always, always click on the View Details link to open the View Details dialog. If the message in the dialog isn't expanded, expand it, then scan down to the Inner Exception entry.

What are internal exceptions?

An internal exception is only visible inside its own internal scope. After the exception falls outside the internal scope, only the base exception can be used to catch the exception.

What is the difference between exception and inner exception?

The InnerException is a property of an exception. When there are series of exceptions, the most current exception can obtain the prior exception in the InnerException property. Let us say we have an exception inside a try block throwing an ArgumentException and the catch clause catches it and writes it to a file.


Video Answer


2 Answers

You can see the code below.

First step, I parse "abc" to integer. It will raise FormatException.

In the catch block, I try to open a text file to log the exception message. But this file didn't exist. FileNotFoundException will be raised.

I want to know what raised the second exception, so I add the first exception (or FormatException) to the constructor of the second exception.

Now the first exception is InnerException of the second exception.

In the catch block, I can access InnerException's properties to know what the first exception is.

Is it useful?

using System; using System.IO; public class Program {     public static void Main( )     {         try         {             try             {                 var num = int.Parse("abc"); // Throws FormatException                            }             catch ( FormatException fe )             {                 try                 {                     var openLog = File.Open("DoesNotExist", FileMode.Open);                 }                 catch                 {                     throw new FileNotFoundException("NestedExceptionMessage: File `DoesNotExist` not found.", fe );                 }                                           }         }         // Consider what exception is thrown: FormatException or FileNotFoundException?         catch ( FormatException fe)         {             // FormatException isn't caught because it's stored "inside" the FileNotFoundException         }         catch ( FileNotFoundException fnfe )          {             string inMes="", outMes;             if (fnfe.InnerException != null)                 inMes = fnfe.InnerException.Message; // Inner exception (FormatException) message             outMes = fnfe.Message;             Console.WriteLine($"Inner Exception:\n\t{inMes}");             Console.WriteLine($"Outter Exception:\n\t{outMes}");         }             } } 

Console Output

Inner Exception:     Input string was not in a correct format. Outter Exception:     NestedExceptionMessage: File `DoesNotExist` not found. 

The Outter exception refers to the most deeply nested exception that is ultimately thrown. The Inner exception refers the most shallow (in scope) exception.

like image 196
Thinh Vu Avatar answered Sep 21 '22 08:09

Thinh Vu


An inner exception is the exception that caused the current exception.

It is used in cases when you want to surface a different exception than the one that your code caught but you don't want to throw away the original context.

In order for a new exception to have information about a previous one, like you said, you pass it as constructor parameter to the new one.

Usually, a null inner exception means that the current exception is root cause of the exceptional situation.

like image 26
Victor Hurdugaci Avatar answered Sep 20 '22 08:09

Victor Hurdugaci