Exception

Exception


This is the base class from which all exception derive their interface.


Implements:

IObject 

Public:

Properties:

NameDescription
 Data (get) Returns a key/value collection used to contain user-defined specific information about the exception.  
 ErrorNumber (get) Returns the VB error number associated with this exception instance.  
 HelpLink (get) Gets a link to a help file associated with the exception.  
 HelpLink (let) Sets a link to a help file associated with the exception.  
 HResult (get) Gets the HRESULT, a coded numerical value that is assigned to a specific exception.  
 InnerException (get) Gets the exception that caused the Subclassed exception to be thrown.  
 Message (get) Gets the error message associated with the Subclass exception.  
 Source (get) Gets a description of the source of the exception.  
 Source (let) Sets a description of the source of the exception.  

Methods:

NameDescription
 Equals Provides a basic implementation of the Equals function of the IObject interface.  
 GetBaseException Gets the original exception that caused the chain of exceptions to occur.  
 GetHashCode Provides a basic implementation of the GetHashcode function of the IObject interface.  
 ToString Returns the exception message prepended with the type name of the Subclass Exception.  

Remarks

This class is the base class for all exceptions. When an error occurs, either the system or the currently executing application reports it by throwing an exception containing information about the error. Once thrown, an exception is handled by the application or by the default exception handler. There are two methods of raising an error: Using the classical VB approach Err.Raise. This method does not place an exception object in holding to be caught. In order to catch the error using the Catch method, call Catch passing in an exception variable and also the Err object. This will create an Exception object with the Err information. Use this method if an error might be raised by code that does not use the Throw method. Using the VBCorLib approach of Throw {exception object}. This method does place the exception in holding to be caught. It then raises an error using the standard Err.Raise method giving the information within the exception object. This allows for the error to be caught by error traps not utilzing the Throw/Catch style. To catch the exception using Catch, an error trap is still required. Once in the trap call Catch passing in an exception variable that is set to the held exception object. Catch will return True if an exception was caught, False otherwise.

Example:

This example demonstrates catching an exception and determining the type of exception caught. The FileNotFoundException type is tested for to show how exception types can be determined.
 Private Sub Form_Load()
     Dim fs As FileStream

     On Error GoTo CatchIt
     Set fs = NewFileStream("missing.txt", OpenExisting)

 CatchIt:
     Dim ex As Exception
     If Catch(ex) Then
         If TypeOf ex Is FileNotFoundException Then
             Call Console.WriteLine("FileNotFoundException Handler: {0}", ex.ToString)
         Else
             Call Console.WriteLine("Generic Exception Handler: {0}", ex.ToString)
         End If
     End If
 End Sub
 

See Also

Project CorLib Overview

Class Exception Overview