VB.NET InnerException

In this article I will explain you about InnerException in VB.NET.
  • 4430

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 below example, demonstrates the concept of checking the inner exception. 

Example of InnerException

Imports System
Imports System.IO

Public Class TestInnerException
    Shared Sub Main()
 New ArgumentException()
            Catch e As ArgumentException
                'make sure this path does not exist
                If File.Exists("\Bigsky\log.txt") = False Then
 New FileNotFoundException("File Not found when trying to write argument exception to the file", e)
                End If
 e As Exception
.WriteLine([String].Concat(e.StackTrace, e.Message))
        If e.InnerException Is Nothing Then
            Console.WriteLine("Inner Exception")
            Console.WriteLine([String].Concat(e.InnerException.StackTrace, e.InnerException.Message))
        End If
    End Sub

Let's explain what happens in above example: As you can see below, in the inner try code block we throw ArgumentException. The inner catch statement will then catch ArgumentException.

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:

The first step is to display e.StackTrace and e.Message in the console:

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:

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. 

Output of above example 



Hope this article would have helped you in understanding InnerException in VB.NET.


More Articles

© 2020 DotNetHeaven. All rights reserved.