Application of error-handling on-error in VB

Source: Internet
Author: User
Tags error code error handling goto

On Error statement

Starts an error handler and specifies the location of the subroutine in a process, or it can be used to suppress an error handler.

Grammar:

On Error GoTo Line

On Error Resume Next

On Error GoTo 0

The syntax of the On Error statement can have any of the following forms: statement description On Error GoTo line start error handler, and the routine starts with the line specified in the required line parameters. The line parameter can be any row label or line number. If a run-time error occurs, the control jumps to line and activates the error handler. The specified line must be in the same procedure as the On Error statement, otherwise a compile-time error occurs. On Error Resume next shows that when a run-time error occurs, the control goes to the statement immediately following the statement where the error occurred and continues to run. This form is used instead of on Error GoTo when accessing an object. On Error GoTo 0 disables any started error handlers in the current procedure. Description if you do not use the On Error statement, any run-time errors are fatal, that is, the result causes an error message to be displayed and the run is aborted.

      an "allowed" error handler is a handler opened by the On Error statement, and an "active" error handler is an error handler that is allowed during the process of the error. The error handler for the current procedure will not be able to handle the error if an error occurs while the error handler is active (between the time the error occurred and the execution of the Resume, exit Sub, Exit Function, or Exit Property statement). Control returns the calling procedure. If the calling procedure has a started error handler, the error handler is activated to handle the error. If the error handler of the calling procedure is also active, the control will then go back to the previous calling procedure, so that it continues until it finds an error handler that is allowed but not active. If an error handler is not found to be allowed and inactive, the error itself is serious where the error actually occurs. Each time the error handler returns the control to the calling procedure, the procedure becomes the current procedure.

      in any procedure, once the error handler has handled the error, it will resume running from the location specified in the Resume statement during the current procedure. Note An error handler is not a Sub procedure or a Function procedure. It is a code marked with a row or line number. The error handler relies on the value in the Number property of the Err object to determine why the error occurred. The error handler should test or store the related property values in the Err object before any other errors occur, or before invoking a procedure that might cause an error to occur. The property values in the Err object reflect only the most recent error. The Err.Description contains error messages that are associated with Err.Number. On Error Resume Next causes the program to continue running from the statement immediately following the statement that produced the error, or from the statement that immediately followed the procedure containing the On Error Resume Next statement. This statement allows the execution of the program to continue without ignoring the run-time error. You can place the error handler where the error occurred without having to transfer the control to another location in the procedure. When another procedure is called, the On Error Resume Next statement becomes inactive, so if you want to embed error handling in the routine, you should execute the On Error Resume Next statement in each invocation routine. Note When dealing with errors that occur during access to other objects, instead of using the On Error GoTo instruction, use on error to Resume Next. Every time you work with an object, checking for ERR will dispel this suspicion when you don't know which object to access with your code. You can determine which object produces an error (the object specified in Err.Source), or determine which object placed the error code in Err.Number. On Error GoTo 0 stops processing errors in the current process. Even if the procedure contains a line numbered 0, it does not specify line 0 as the starting point for the code that handles the error.

If there is no on Error GoTo 0 statement, the error handler shuts down automatically when the procedure is exited. To prevent error handler code from running when the error does not occur, write the exit Sub, Exit Function, or Exit Property statement immediately before the error handler, as in the next procedure.   Sub Initializematrix (Var1, Var2, Var3, VAR4) on Error GoTo ErrorHandler ...   Exit SubErrorHandler: ... Resume NextEnd Sub Here, the error handler code is separated from the process by the Exit Sub statement, and before the END Sub statement. The error handler code can be written anywhere in the program. When an object is run as a file, an uncaught error in the object is returned to the control application. In the development environment, if the correct option is set, the uncaught error returns only the control application. Refer to the documentation for the main application for a description of what options should be set during debugging, how to set these options, and whether the host can establish a class. If you create an object that accesses other objects, you should proceed to handle the unhandled errors returned from those objects. If this error cannot be handled, treat the error code in Err.Number as an error, and then pass the error back to the caller of the object. The error code should be added to the vbObjectError constant to specify the error.

For example, if the error code is 1052, the error is specified using the following method:

Err.Number = vbObjectError + 1052

Note system errors that occur during the invocation of a dynamic-link library (DLL) do not result in exceptions and are not captured by the Visual Basic error capture operation. When invoking a DLL function, each return value should be checked (as detailed in the API) to determine whether it succeeded or failed, and if it fails, check the value in the LastDLLError property of the Err object.

On Error GoTo 0
Represents a ban on any started error handlers in the current procedure.

On Error Resume Next
Description When a run-time error occurs, the control goes to the statement immediately following the statement where the error occurred and continues to run. This form is used instead of on Error GoTo when accessing an object.

On Error GoTo Line
Starts the error handler, and the routine starts with the line specified in the necessary line parameters. The line parameter can be any row label or line number. If a run-time error occurs, the control jumps to line and activates the error handler. The specified line must be in the same procedure as the On Error statement, otherwise a compile-time error occurs.

The explanations above on MSDN

In general, if you make an error capture of an object or control that we create, you need to use on error to Resume Next, and then in judging its err.number, do the appropriate processing according to the type of error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.