Freigeben über


Using User-Filtered Exception Handlers 

Currently, Visual Basic supports user-filtered exceptions. User-filtered exception handlers catch and handle exceptions based on requirements you define for the exception. These handlers use the Catch statement with the When keyword.

This technique is useful when a particular exception object corresponds to multiple errors. In this case, the object typically has a property that contains the specific error code associated with the error. You can use the error code property in the expression to select only the particular error you want to handle in that Catch clause.

The following Visual Basic example illustrates the Catch/When statement.

Try
      'Try statements.
   Catch When Err = VBErr_ClassLoadException
      'Catch statements.
End Try

The expression of the user-filtered clause is not restricted in any way. If an exception occurs during execution of the user-filtered expression, that exception is discarded and the filter expression is considered to have evaluated to false. In this case, the common language runtime continues the search for a handler for the current exception.

Combining the Specific Exception and the User-Filtered Clauses

A catch statement can contain both the specific exception and the user-filtered clauses. The runtime tests the specific exception first. If the specific exception succeeds, the runtime executes the user filter. The generic filter can contain a reference to the variable declared in the class filter. Note that the order of the two filter clauses cannot be reversed.

The following Visual Basic example shows the specific exception ClassLoadException in the Catch statement as well as the user-filtered clause using the When keyword.

Try
      'Try statements.
   Catch cle As ClassLoadException When cle.IsRecoverable()
      'Catch statements.
End Try

See Also

Tasks

How to: Use the Try/Catch Block to Catch Exceptions
How to: Use Specific Exceptions in a Catch Block

Concepts

Best Practices for Handling Exceptions

Other Resources

Exception Handling Fundamentals