ProblemYou have a block of code that might generate errors, but you don't really care. You want the code to continue on with or without errors and to provide no error report to the user. SolutionTo ignore errors, use the On Error Resume Next statement, or use a TRy statement with an empty Catch block. DiscussionIn Visual Basic, the traditional way to ignore errors in a section of code is to use the On Error Resume Next statement. The following code shows both ignored and pro-cessed error-handler sections: Public Sub DoSomething( ) On Error Resume Next ' ----- Error handling is now disabled. You can do ' dangerous things and no errors will occur. The ' "Err" object will still be filled in with ' error content when an error does occur, so you ' can check that if you are concerned. On Error GoTo ErrorHandler ' ----- Error handling has been turned back on. All ' errors will jump down to the labeled section. Exit Sub ErrorHandler: ' ----- Do something with the error here, then… Resume Next End Sub If you want to ignore errors but prefer using the structured exception-handling features, add a TRy block with an empty Catch block: Public Sub DoSomething( ) Try ' ----- As expected, any error that occurs here will ' jump to the Catch block. Catch ' ----- If you don't include any error-handling code ' here, the error is just ignored. End Try ' ----- Errors that occur out here will not be caught by ' the Try block, but you knew that already. End Sub There is a small difference between these two blocks of code. When using the On Error Resume Next statement, any error on a statement causes the code to continue with the next statement. In the TRy…Catch example, any error that occurs in the TRy block causes the code to continue with the Catch block, and then with the code that follows the entire try…End Try section. This means that if you have multiple statements in the try block and an error occurs on the first of those statements, the remaining statements in the try block are skipped completely. |