Section 17.9. Custom Exceptions

   

17.9 Custom Exceptions

The intrinsic exception types the CLR provides, coupled with the custom messages shown in the previous example, will often be all you need to provide extensive information to a catch block when an exception is thrown.

There will be times, however, when you want to provide more extensive information to or need special capabilities in your exception. It is a trivial matter to create your own custom exception class; the only restriction is that it must derive (directly or indirectly) from System.ApplicationException. Example 17-7 illustrates the creation of a custom exception.

Example 17-7. A custom exception
 Option Strict On Imports System Namespace ExceptionHandling     ' custom exception class     Public Class MyCustomException         Inherits System.ApplicationException         Public Sub New(ByVal message As String)             ' pass the message up to the base class             MyBase.New(message)         End Sub 'New     End Class 'MyCustomException     Class Tester         Public Sub Run( )             Try                 Console.WriteLine("Open file here")                 Dim a As Double = 0                 Dim b As Double = 5                 Console.WriteLine("{0} / {1} = {2}", a, b, DoDivide(a, b))                 Console.WriteLine("This line may or may not print")                 ' most derived exception type first             Catch e As System.DivideByZeroException                 Console.WriteLine( _                    "DivideByZeroException! Msg: {0}", e.Message)                 Console.WriteLine("HelpLink: {0}", e.HelpLink)                 ' catch custom exception              Catch e As MyCustomException                 Console.WriteLine( _                    "MyCustomException! Msg: {0}", e.Message)                 Console.WriteLine("HelpLink: {0}", e.HelpLink)             Catch ' catch any uncaught exceptions                 Console.WriteLine("Unknown exception caught")             Finally                 Console.WriteLine("Close file here.")             End Try         End Sub 'Run         ' do the division if legal         Public Function DoDivide( _             ByVal a As Double, ByVal b As Double) As Double             If b = 0 Then                 Dim e As New DivideByZeroException( )                 e.HelpLink = "http://www.libertyassociates.com"                 Throw e             End If             If a = 0 Then                 ' create a custom exception instance                 Dim e As New _                   MyCustomException("Can't have zero divisor")                 e.HelpLink = _                    "http://www.libertyassociates.com/NoZeroDivisor.htm"                 Throw e             End If             Return a / b         End Function 'DoDivide         Shared Sub Main( )             Console.WriteLine("Enter Main...")             Dim t As New Tester( )             t.Run( )             Console.WriteLine("Exit Main...")         End Sub 'Main     End Class 'Tester End Namespace 'ExceptionHandling 
  Output:  Enter Main... Open file here MyCustomException! Msg: Can't have zero divisor HelpLink: http://www.libertyassociates.com/NoZeroDivisor.htm Close file here. Exit Main... 

MyCustomException is derived from System.ApplicationException and consists of nothing more than a constructor that takes a string message that it passes to its base class.

Remember that constructors cannot be inherited, so every derived class must have its own constructor.

The advantage of creating this custom exception class is that it better reflects the particular design of the Test class, in which it is not legal to have a zero divisor. Using the ArithmeticException rather than a custom exception would work as well, but it might confuse other programmers because a zero divisor wouldn't normally be considered an arithmetic error.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net