ExceptionMethods: Throw

Throw

Raises an error using the supplied exception object. The exception is then set and ready to be caught using Catch.



 Public Sub Throw(
	  Optional ByVal Ex As Object )

Parameters

Ex
[ByVal] Optional. Object. The exception object used to raise an error. This will accept any object that implements the Exception interface or an ErrObject. All exception classes in VBCorLib implement Exception.

Remarks

VBCorLib provides a wide range of exception objects that can be used to define specific errors that occur during the execution of an application. In order to use the exception objects through VBCorLib, they must first be Thrown. Throwing an exception object is similar to raising an error using Visual Basics standard method of Err.Raise.

To throw an exception, it must be passed into the Throw function. This will store the exception object so it can be retrieved (Caught) during error trapping.

 ' Throwing an exception directly is the simplest method.
 Throw New Exception
 
This creates a new Exception object and passes it directly into the Throw function. The function then sets the object to be caught and raises an error using the HResult, Source, and Message properties. These properties corrispond to the Err.Number, Err.Source, and Err.Description parameters used when raising an error.

Exceptions that are to be thrown should be constructed with meaningful information before being passed into the Throw function.

An Exception that occurs but is not caught using Catch can be rethrown by calling Throw without any parameters. This causes the Throw function to check if a pending Exception already exists, and if so, rethrow it. This does not clear an exception.

See Also

Project CorLib Overview

Class ExceptionMethods Overview

Constructors

Exception