Discussion Locked

Error Handling in vbscript

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello friend I am trying to use error handling machanism of vbscript to traps errors.
My code is like this.

sub myProc
On Error Goto MyErrorHandler
code of the procedure
again code of the procedure

exit sub MyErrorHandler:
code to handle error
few more lines to handle code
end sub

My problem is i am getting syntax error on the line where my "On Error Goto MyErrorHandler" statment is present. And on thisline it is pointing at the begining of MyErrorHandler Label.

Please solve my problem

3 replies

by andrew

Unfortunately, VBScript does not provide 'On Error Goto ...' syntax.

If you need robust error handling, you may consider moving that logic to a VB component.

If you need to keep to straight VBScript, use syntax like the following:

Sub Proc()
On Error Resume Next

'do something

If Err.Number <> 0 then
'handle the error
End If

End Sub

The idea is to turn off built-in error checking and explicitly test for errors after critical code has executed.

It's not great, but it does work.

This discussion is closed to new replies.