Skip to Content
Programming Visual Basic .NET, Second Edition
book

Programming Visual Basic .NET, Second Edition

by Jesse Liberty
April 2003
Intermediate to advanced
560 pages
14h 4m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET, Second Edition

Rethrowing Exceptions

You might want your catch block to take some initial corrective action and then rethrow the exception to an outer try block (in a calling function). It might rethrow the same exception, or it might throw a different one. If it throws a different one, it may want to embed the original exception inside the new one so that the calling method can understand the exception history. The InnerException property of the new exception retrieves the original exception.

Because the InnerException is also an exception, it too might have an inner exception. Thus, an entire chain of exceptions can be nested one within the other, much like Ukrainian dolls are contained one within the other. Example 11-8 illustrates.

Example 11-8. Rethrowing and inner exceptions

Option Strict On Imports System Namespace Programming_VBNET Public Class MyCustomException Inherits System.ApplicationException Public Sub New(ByVal message As String, ByVal inner As Exception) MyBase.New(message, inner) End Sub 'New End Class 'MyCustomException Public Class Test Public Shared Sub Main( ) Dim t As New Test( ) t.TestFunc( ) End Sub 'Main Public Sub TestFunc( ) Try DangerousFunc1( ) ' if you catch a custom exception ' print the exception history Catch e As MyCustomException Console.WriteLine(ControlChars.Lf + "{0}", e.Message) Console.WriteLine("Retrieving exception history...") Dim inner As Exception = e.InnerException While Not (inner Is Nothing) Console.WriteLine("{0}", inner.Message) inner = inner.InnerException ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET

Programming Visual Basic .NET

Dave Grundgeiger
VB.NET Language in a Nutshell, Second Edition

VB.NET Language in a Nutshell, Second Edition

Steven Roman PhD, Ron Petrusha, Paul Lomax

Publisher Resources

ISBN: 0596004389Supplemental ContentCatalog PageErrata