Suppress errors for the whole script

Jonathan Rioux picture Jonathan Rioux · Nov 29, 2012 · Viewed 11.6k times · Source

I am want to suppress all the errors that could appear in my VBS logon script.

Can I surround the WHOLE 500 lines script with :

On Error Resume Next

'[... whole script (~500 lines of code) ...]

On Error GoTo 0

Answer

Ekkehard.Horner picture Ekkehard.Horner · Nov 29, 2012

You can do it - even without the OEG0 line - but you shouldn't, because the script will continue to execute lines i ... last, even if an error in line i-1 invalidates all your assumptions about necessary pre-conditions of the actions in those lines. Your strategy is comparable to driving with your eyes shut to avoid being dazzled by the headlights of other cars.

If you can't do locally resticted error handling for selected actions -

...
On Error Resume Next
  risky_action
  save Err
On Error GoTo 0
If ErrorOccurred Then
   something sensible
   If can't continue Then
      WScript.Quit 4711
   End If
End If
...

try to get away with

Sub Main()
  ... you 500 lines ...
End Sub 

On Error Resume Next
  Main
  If Err.Number Then
     WScript.Echo "aborted"
     WScript.Quit 4711
  End If

This approach makes sure that the lines after an error won't be executed.