开发者

Can I throw an error in vbscript?

开发者 https://www.devze.com 2023-01-31 11:29 出处:网络
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 Erro

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 exampl开发者_StackOverflow社区e 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.


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).


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消