Appendix F. Error Handling
This appendix provides information on error handling.
Structured Error Handling
A Try block tries to execute some code and reacts to errors. The syntax is as follows:
Trytry_statements...[Catch ex Asexception_type_1 exception_statements_1...] [Catch ex Asexception_type_2 exception_statements_2...] ... [Catchfinal_exception_statements...] [Finallyfinally_statements...] End Try
When an error occurs, the program examines the Catch statements until it finds one that matches the current exception. The program executes the finally_statements after the try_statements succeed or after any Catch block is done executing.
Throwing Exceptions
Use the Throw statement to throw an exception, as in the following code:
Throw New ArgumentException("Width must be greater than zero")Exception classes provide several overloaded constructors so you can indicate such things as the basic error message, the name of the variable that caused the exception, and an inner exception.
For information on useful exception classes and custom exception classes, see Appendix O, "Useful Exception Classes."
Classic Error Handling
The On Error statement controls error handlers in Visual Basic classic error handling. You can use structured and classic error handling in the same program but not in the same routine.
The following list briefly describes the On Error statement's four variations:
On Error GoToline— If an error occurs, the program enters error-handling mode and control jumps to the indicated ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access