35

I'm used to programing in C#, which obviously has some pretty robust error handling. Now I'm working on a short project in VBScript. I've read about the error handling with VBscript using "On Error _______" and the Err global variable.

However, is there some way I can generate my own errors? For example if one of my functions is passed the wrong type of parameter I'd rather my script just flat out fail at that point then try to continue.

3 Answers 3

38

While @helen's answer is correct it is a bit sparse.

It details the Err.Raise() method but misses out some key points.

From the original question
However, is there some way I can generate my own errors? For example if one of my functions is passed the wrong type of parameter I'd rather my script just flat out fail at that point then try to continue.

Err.Raise() is extremely versatile it can throw existing exceptions (as already discussed) but it can also throw completely new user-defined exceptions.

Call Err.Raise(vbObjectError + 10, "My Application", "My custom error message")

The vbObjectError (-2147221504) is an in-built VBScript constant that defines the boundary for raising user-defined errors.

3
  • 1
    The user-defined info very helpful.
    – bvj
    Commented Oct 14, 2019 at 7:32
  • 1
    During testing I found that you should that the custom error number should be in the range replace the custom error number should be in the range 513–65535 (so not 10), it does work but the custom error message is not to be found on the ASP error object (more info: learn.microsoft.com/en-us/office/vba/language/reference/…).
    – Sunib
    Commented Nov 13, 2020 at 9:59
  • @Sunib thanks for the reference, but honestly I've never had any issues using vbObjectError and any integer. But thank you for pointing out the range anyway, could be useful to somebody.
    – user692942
    Commented Nov 15, 2020 at 1:38
36

Yes, you can. Use the Err.Raise method, e.g.:

Err.Raise 5 ' Invalid procedure call or argument

For a list of VBScript's standard error codes, see Errors (VBScript).

0
4

C# try-catch-finally

try {
    // some code
} catch( Exception e ) {
    // error handler
} finally {
    // clean up things
}

VBScript equivalent

on error resume next
' some code
if( Err.number <> 0 ) then
    ' error handler -- you can use Err.raise() here to display your own errors
    Err.clear()
else
    ' clean up things
end if
on error goto 0

For good VBScript examples, you could check the ASP Xtreme Evolution project

7
  • I think the best practice is to wrap your error handling logic in a label rather than using On Error Resume Next. Commented Dec 21, 2010 at 19:43
  • just to make a parallel with C# Commented Dec 21, 2010 at 19:46
  • 8
    @ChaosPandion On Error GoTo Label isn't supported in VBScript.
    – Tmdean
    Commented Dec 22, 2010 at 2:03
  • @Tmdean - After a while VB6 and VBScript kinda blend together. Commented Dec 22, 2010 at 13:20
  • 8
    These two codes blocks are not equivalent because a finally block executes even when the error occurs. Your VBScript "equivalent" should have the 'clean up things part outside the If statement, and an On Error GoTo 0 to stop the error handling. Furthermore, another difference is that the VBScript 'some code part can only be one line long, or at least can contain only one line that could generate an error, because the very next line, no matter what it is will be executed when On Error Resume Next is active. If 1 = 1 / 0 Then will flow INTO the If block.
    – ErikE
    Commented Sep 3, 2012 at 4:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.