Exception Class

   
Exception Class

Namespace

System

Createable

Yes

Description

The Exception class and its inherited (child) classes represent runtime exceptions.

Selected Exception Class Members

The following provides a brief description of the more important members of the Exception class:

HelpFile property

Sets or retrieves a link to the help file associated with the exception. Its value is a Uniform Resource Name (URN) or Uniform Resource Locator (URL).

InnerException property

Returns a reference to the inner Exception object in the case of nested exceptions.

Message property

Returns the text of the error message.

Source property

Returns or sets a string containing the name of the application or the object that causes the error.

StackTrace property

Returns a string (the stack trace) consisting of a list of all methods that are currently in the stack. The following shows a stack trace when the procedure DoArithmetic calls the procedure Arithmetic , which generates an exception that is thrown up to DoArithmetic (the string has been formatted to fit the margins of the page):

 at WindowsApplication6.Form1.Arithmetic(String Action, Double x,  Double y) in C:\Projects\WindowsApplication6\Form1.vb:line 68 at WindowsApplication6.Form1.DoArithmetic(  ) in  C:\Projects\WindowsApplication6\Form1.vb:line 87 
TargetSite property

Returns a MethodBase object representing the method that throws the exception. For example, if e is the exception whose stack trace is shown in the discussion of the StackTrace property, then the code.

 e.TargetSite.Name 

will return the string Arithmetic .

GetBaseException Method

This method returns the exception object for the innermost exception. For instance, in the previous example (see the discussion of the StackTrace property) the code:

 e.GetBaseException.ToString 

returns the string:

 System.ArithmeticException: There was an overflow or  underflow in the arithmetic operation.    at WindowsApplication6.Form1.Arithmetic(String Action,  Double x, Double y) in  C:\Projects\WindowsApplication6\Form1.vb:line 68    at WindowsApplication6.Form1.DoArithmetic(  ) in  C:\Projects\WindowsApplication6\Form1.vb:line 87/// 
ToString Method

Returns the fully qualified name of the exception and possibly the error message, the name of the inner exception, and the stack trace.

Children of the Exception Class

The System namespace contains the Exception class, which is the base class for a substantial collection of derived exception classes, listed as follows . Note that the indentation indicates class inheritance. For example, EntryPointNotFoundException (the fifth from the last entry in the list) inherits from TypeLoadException.

  Exception     ApplicationException     SystemException        AccessException           FieldAccessException           MethodAccessException           MissingMemberException              MissingFieldException              MissingMethodException        AppDomainUnloadedException        AppDomainUnloadInProgressException        ArgumentException           ArgumentNullException           ArgumentOutOfRangeException           DuplicateWaitObjectException        ArithmeticException           DivideByZeroException           NotFiniteNumberException           OverflowException        ArrayTypeMismatchException        BadImageFormatException        CannotUnloadAppDomainException        ContextMarshalException        CoreException           ExecutionEngineException           IndexOutOfRangeException            StackOverflowException        ExecutionEngineException        FormatException        InvalidCastException        InvalidOperationException        MulticastNotSupportedException        NotImplementedException        NotSupportedException           PlatformNotSupportedException        NullReferenceException        OutOfMemoryException        RankException        ServicedComponentException        TypeInitializationException        TypeLoadException           EntryPointNotFoundException        TypeUnloadedException        UnauthorizedAccessException        WeakReferenceException URIFormatException  

Programming Tips and Gotchas

  • As Microsoft states: "Most of the exception classes that inherit from Exception do not implement additional members or provide additional functionality." Thus, it is simply the class name that distinguishes one type of exception from another. The properties and methods applied to an exception object are inherited from the Exception base class.

  • You can trap the generic Exception object, or you can trap a specific exception object. There are two circumstances in particular when you may want to trap a specific exception, rather than the more general Exception object:

    • You want to handle errors differently based on their class. For instance, you may want to issue different custom error messages for different exception types.

    • You want to take advantage of members of a particular exception class that are not implemented in the Exception base class. For instance, the ArgumentException class has a ParamName property that returns the name of the parameter that causes the exception. If you trap the Exception class rather than the ArgumentException class, this member is unavailable.

VB.NET/VB 6 Differences

The Exception class, along with Structured Exception Handling (SEH), is new to the .NET platform.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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