This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.
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. However, if the file path is not found, FileNotFoundException is thrown. Let's say that the outside try block catches this exception, but how about the actual ArgumentException that was thrown? Is it lost? No, the InnerException property contains the actual exception. This is the reason for the existence of an InnerException property for any exception.
The following example, Listing 7.13, demonstrates the concept of checking the inner exception.
Listing 7.13: Exception13.cs, InnerException Example
using System;
using System.IO;
public class TestInnerException
{
public static void Main()
{
try
{
try
{
throw new ArgumentException();
}
catch (ArgumentException e)
{
//make sure this path does not exist
if (File.Exists("file://Bigsky//log.txt%22)%20==%20false) == false)
{
throw new FileNotFoundException("File Not found when trying to write argument exception to the file", e);
}
}
}
catch (Exception e)
{
Console.WriteLine(String.Concat(e.StackTrace, e.Message));
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception");
Console.WriteLine(String.Concat(e.InnerException.StackTrace, e.InnerException.Message));
}
}
Console.ReadLine();
}
}
Let's explain what happens in Listing 7.13: As you can see below, in the inner try code block we throw ArgumentException. The inner catch statement will then catch ArgumentException.
catch(ArgumentException e)
{
//make sure this path does not exist
if (File.Exists("file://Bigsky//log.txt%22)==false)==false)
{
throw new FileNotFoundException("File Not found when trying to write argument exception to the file",e);
}
}
The Exists method of the File class is used to check if the file exists. If the file does not exist, we again throw a new FileNotFoundException with the user-entered exception message. The outer catch will catch the inner exception. The code below is the outer catch block, which we'll explain step by step:
catch(Exception e)
{
Console.WriteLine(String.Concat(e.StackTrace,e.Message));
if (e.InnerException!=null)
{
Console.WriteLine("Inner Exception");
Console.WriteLine(String.Concat(e.InnerException.StackTrace,
e.InnerException.Message));
}
}
The first step is to display e.StackTrace and e.Message in the console:
Console.WriteLine(String.Concat(e.StackTrace,e.Message));
The e.StackTrace property gives us the location of the error and has the value of at TestInnerException.Main() in g:\temp\consoleapplication4\class1.cs:line 18.
The e.Message property will have the value of File Not found when trying to write argument exception to the file.
After that we check if the InnerException is null or not. If it is not null, we can display InnerException.StackTrace and InnerException.Message in the console:
if (e.InnerException!=null)
{
Console.WriteLine("Inner Exception");
Console.WriteLine(String.Concat(e.InnerException.StackTrace, e.InnerException.Message));
}
The e.InnerException.StackTrace property will have the value of at TestInnerException.Main() in g:\temp\consoleapplication4\class1.cs:line 11.
The e.InnerException.Message property will have the value of Value does not fall within the expected range.
Figure 7.9 shows the output of the code in Listing 7.13.
Figure 7.9: Output of Listing 7.13
Conclusion
Hope this article would have helped you in understanding InnerException in C#. See other articles on the website on .NET and C#.
|
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer. |