On error go to

on error go to

www.automateexcel.com › vba › error-handling. Statement, Description. On Error GoTo line, Enables the error-handling routine that starts at line specified in the required line argument. When On Error Goto is triggered by an error, it is automatically disabled. This means that the occurrence of a second error causes a program abort.

watch the video

Excel VBA Hata Kontrolü - On Error goto

On error go to - was specially

GoTo 0

On Error statement

  • Article
  • 6 minutes to read

Enables an error-handling routine and specifies the location of the routine within a procedure; can also be used to disable an error-handling routine.

Syntax

On Error GoToline
On Error Resume Next
On Error GoTo 0

The On Error statement syntax can have any of the following forms:

StatementDescription
On Error GoTolineEnables the error-handling routine that starts at line specified in the required lineargument.

The line argument is any line label or line number.

If a run-time error occurs, control branches to line, making the error handler active.

The specified line must be in the same procedure as the On Error statement; otherwise, a compile-time error occurs.
On Error Resume NextSpecifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred and execution continues. Use this form rather than On Error GoTo when accessing objects.
On Error GoTo 0Disables any enabled error handler in the current procedure.

If you don't use an On Error statement, any run-time error that occurs is fatal; that is, an error message is displayed and execution stops.

An "enabled" error handler is one that is turned on by an On Error statement; an "active" error handler is an enabled handler that is in the process of handling an error. If an error occurs while an error handler is active (between the occurrence of the error and a Resume, Exit Sub, Exit Function, or Exit Property statement), the current procedure's error handler can't handle the error. Control returns to the calling procedure.

If the calling procedure has an enabled error handler, it is activated to handle the error. If the calling procedure's error handler is also active, control passes back through previous calling procedures until an enabled, but inactive, error handler is found. If no inactive, enabled error handler is found, the error is fatal at the point at which it actually occurred.

Each time the error handler passes control back to a calling procedure, that procedure becomes the current procedure. After an error is handled by an error handler in any procedure, execution resumes in the current procedure at the point designated by the Resume statement.

Note

An error-handling routine is not a Sub procedure or Function procedure. It's a section of code marked by a line label or line number.

Error-handling routines rely on the value in the Number property of the Err object to determine the cause of the error. The error-handling routine should test or save relevant property values in the Err object before any other error can occur or before a procedure that might cause an error is called. The property values in the Err object reflect only the most recent error. The error message associated with Err.Number is contained in Err.Description.

On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement. This statement allows execution to continue despite a run-time error. You can place the error-handling routine where the error would occur, rather than transferring control to another location within the procedure. An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine.

Note

The On Error Resume Next construct may be preferable to On Error GoTo when handling errors generated during access to other objects. Checking Err after each interaction with an object removes ambiguity about which object was accessed by the code. You can be sure which object placed the error code in Err.Number, as well as which object originally generated the error (the object specified in Err.Source).

On Error GoTo 0 disables error handling in the current procedure. It doesn't specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0. Without an On Error GoTo 0 statement, an error handler is automatically disabled when a procedure is exited.

To prevent error-handling code from running when no error has occurred, place an Exit Sub, Exit Function, or Exit Property statement immediately before the error-handling routine, as in the following fragment:

Here, the error-handling code follows the Exit Sub statement and precedes the End Sub statement to separate it from the procedure flow. Error-handling code can be placed anywhere in a procedure.

Untrapped errors in objects are returned to the controlling application when the object is running as an executable file. Within the development environment, untrapped errors are only returned to the controlling application if the proper options are set. See your host application's documentation for a description of which options should be set during debugging, how to set them, and whether the host can create classes.

If you create an object that accesses other objects, you should try to handle errors passed back from them unhandled. If you cannot handle such errors, map the error code in Err.Number to one of your own errors, and then pass them back to the caller of your object. You should specify your error by adding your error code to the vbObjectError constant. For example, if your error code is 1052, assign it as follows:

Note

System errors during calls to Windows dynamic-link libraries (DLL) or Macintosh code resources don't raise exceptions and cannot be trapped with Visual Basic error trapping. When calling DLL functions, you should check each return value for success or failure (according to the API specifications), and in the event of a failure, check the value in the Err object's LastDLLError property. LastDLLError always returns zero on the Macintosh.

Example

This example first uses the On Error GoTo statement to specify the location of an error-handling routine within a procedure. In the example, an attempt to delete an open file generates error number 55. The error is handled in the error-handling routine, and control is then returned to the statement that caused the error. The On Error GoTo 0 statement turns off error trapping.

The On Error Resume Next statement is then used to defer error trapping so that the context for the error generated by the next statement can be known for certain. Note that Err.Clear is used to clear the Err object's properties after the error is handled.

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

On Error Statement (Visual Basic)

  • Article
  • 7 minutes to read

Enables an error-handling routine and specifies the location of the routine within a procedure; can also be used to disable an error-handling routine. The statement is used in unstructured error handling and can be used instead of structured exception handling. Structured exception handling is built into .NET, is generally more efficient, and so is recommended when handling runtime errors in your application.

Without error handling or exception handling, any run-time error that occurs is fatal: an error message is displayed, and execution stops.

Note

The keyword is also used in the Error Statement, which is supported for backward compatibility.

Syntax

Parts

TermDefinition
lineEnables the error-handling routine that starts at the line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to the specified line, making the error handler active. The specified line must be in the same procedure as the statement or a compile-time error will occur.
Disables enabled error handler in the current procedure and resets it to .
Disables enabled exception in the current procedure and resets it to .
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred, and execution continues from that point. Use this form rather than when accessing objects.

Note

We recommend that you use structured exception handling in your code whenever possible, rather than using unstructured exception handling and the statement. For more information, see Try...Catch...Finally Statement.

An "enabled" error handler is one that is turned on by an statement. An "active" error handler is an enabled handler that is in the process of handling an error.

If an error occurs while an error handler is active (between the occurrence of the error and a , , , or statement), the current procedure's error handler cannot handle the error. Control returns to the calling procedure.

If the calling procedure has an enabled error handler, it is activated to handle the error. If the calling procedure's error handler is also active, control passes back through previous calling procedures until an enabled, but inactive, error handler is found. If no such error handler is found, the error is fatal at the point at which it actually occurred.

Each time the error handler passes control back to a calling procedure, that procedure becomes the current procedure. Once an error is handled by an error handler in any procedure, execution resumes in the current procedure at the point designated by the statement.

Note

An error-handling routine is not a procedure or a procedure. It is a section of code marked by a line label or a line number.

Number Property

Error-handling routines rely on the value in the property of the object to determine the cause of the error. The routine should test or save relevant property values in the object before any other error can occur or before a procedure that might cause an error is called. The property values in the object reflect only the most recent error. The error message associated with is contained in .

Throw Statement

An error that is raised with the method sets the property to a newly created instance of the Exception class. In order to support the raising of exceptions of derived exception types, a statement is supported in the language. This takes a single parameter that is the exception instance to be thrown. The following example shows how these features can be used with the existing exception handling support:

Notice that the statement traps all errors, regardless of the exception class.

On Error Resume Next

causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the statement. This statement allows execution to continue despite a run-time error. You can place the error-handling routine where the error would occur rather than transferring control to another location within the procedure. An statement becomes inactive when another procedure is called, so you should execute an statement in each called routine if you want inline error handling within that routine.

Note

The construct may be preferable to when handling errors generated during access to other objects. Checking after each interaction with an object removes ambiguity about which object was accessed by the code. You can be sure which object placed the error code in , as well as which object originally generated the error (the object specified in ).

On Error GoTo 0

disables error handling in the current procedure. It doesn't specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0. Without an statement, an error handler is automatically disabled when a procedure is exited.

On Error GoTo -1

disables the exception in the current procedure. It does not specify line -1 as the start of the error-handling code, even if the procedure contains a line numbered -1. Without an statement, an exception is automatically disabled when a procedure is exited.

To prevent error-handling code from running when no error has occurred, place an , , or statement immediately before the error-handling routine, as in the following fragment:

Here, the error-handling code follows the statement and precedes the statement to separate it from the procedure flow. You can place error-handling code anywhere in a procedure.

Untrapped Errors

Untrapped errors in objects are returned to the controlling application when the object is running as an executable file. Within the development environment, untrapped errors are returned to the controlling application only if the proper options are set. See your host application's documentation for a description of which options should be set during debugging, how to set them, and whether the host can create classes.

If you create an object that accesses other objects, you should try to handle any unhandled errors they pass back. If you cannot, map the error codes in to one of your own errors and then pass them back to the caller of your object. You should specify your error by adding your error code to the constant. For example, if your error code is 1052, assign it as follows:

Caution

System errors during calls to Windows dynamic-link libraries (DLLs) do not raise exceptions and cannot be trapped with Visual Basic error trapping. When calling DLL functions, you should check each return value for success or failure (according to the API specifications), and in the event of a failure, check the value in the object's property.

Example

This example first uses the statement to specify the location of an error-handling routine within a procedure. In the example, an attempt to divide by zero generates error number 6. The error is handled in the error-handling routine, and control is then returned to the statement that caused the error. The statement turns off error trapping. Then the statement is used to defer error trapping so that the context for the error generated by the next statement can be known for certain. Note that is used to clear the object's properties after the error is handled.

Requirements

Namespace:Microsoft.VisualBasic

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See also

Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.