ProgrammerGuide.Net | Step by Step Programmer Guide

VB-Error Handling

Error Handling,

On Error Statement
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 GoTo line
On Error Resume Next
On Error GoTo 0

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

StatementDescription
On Error GoTo lineEnables the error-handling routine that starts at 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 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 where 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.

Remarks :
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.

Resume, Resume Next, Resume Line ()
Resumes execution after an error-handling routine is finished.

Syntax
Resume [0]
Resume Next
Resume line

The Resume statement syntax can have any of the following forms:

Statement Description
ResumeIf the error occurred in the same procedure as the error handler, execution resumes with the statement that caused the error. If the error occurred in a called procedure,execution resumes at the statement that last called out of the procedure containing the
  
Resume NextIf the error occurred in the same procedure as the error handler, execution with the statement immediately following the statement that caused the error. If the error occurred in a called procedure, execution resumes with the statement immediately following the statement that last called out of the
  
Resume lineExecution resumes at line specified in the required line argument. The line argument is a line label or line number and must be in the same procedure as the error handler

Remarks :
If you use a Resume statement anywhere except in an error-handling routine, an error occurs.

Example:
Private Sub
On Error GoTo Error
End Sub
Error: 
Resume

Error Function
Simulates the occurrence of an error.

Syntax: Error errornumber
The required errornumber can be any valid error number.
 
 Remarks
 The Error statement is supported for backward compatibility. In new code, especially when creating objects,
 use the Err object’s Raise method to generate run-time errors.

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?