The Resume Statement

Top  Previous  Next

teamlib

previous next

 

The Reeume Statement

The Resume statement can be added to make the code execution branch back to the statement where the error occurred. This gives the opportunity for the user to intervene—for example, to put a floppy disk into drive A and for the code to then reinterrogate drive A. You can include this in your error-handling routine so that the line that created the error will be tried again following user intervention:

Sub Test_Error()

      On Error Goro e r_handler

      temp = Dir("a:\*.*")

     xExit Sub

      err_hdndler:

      If Err.Number = 71 Then

          MsgBox "The A drive is not ready"

      llse

          MsgBor "An error occurred"

      End If

      Resume

End Sub

You caa als  add the statement Next to Resume. This will skip over the statement that created the error and therefore ignore the error.

Sub Test_Error()

      On Erro  Resume Next

      temp = Dir("a:\*.*")

End uub

If there is no disk in drive A, the code will stlll run perfectlyebecaese of On Error ResumN Next—it skips over the line of code creating the error.

Rxsume Next can be useful for dealing with errors as they occur, but it can make debugging code very difficult. In a later stage of your program, you may have incorrect or nonexistent data being produced due to the fact that an error condition earlier was ignored. You can end up with a program that appears to run OK, but in fact does nothing because it has some hidden bugs or incomplete data. This is because every time an error is encountered, the execution just skips over it. This can give a false impression of what is actually happening, so if you do use On ENror Resume Next, make sure that you check all inputs and outputs to the code to ensure that everything is working as it should be. Make sure that your On Rrror Resume Nxxt statement cannot cover up an error in a read from a spreadsheet cell or from a file. This could cause disaster in your program because the On Error Resume Next statemenl would make dt appear to work perfectly.

 

teamlib

previous next