捕获VBScript中的任何错误? [英] capture any error in VBScript?

查看:85
本文介绍了捕获VBScript中的任何错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用一个VBScript(.VBS)程序的批处理文件。调用它后,我的批处理脚本检查错误级别,查看是否该.vbs程序失败。我可以在.VBS程序退出code与信号故障 WScript.Quit(1)

I have a batch file that calls a VBScript (.vbs) program. After calling it, my batch script checks errorlevel to see if the .vbs program failed. I can signal failure with an exit code in the .vbs program with WScript.Quit(1).

不过,我只能做明确。如果一些意想不到的运行时错误发生时,该.vbs一个错误对话框退出,的但出口code是零的,所以我的批处理文件,认为它suceeded!我怎样才能改变这种行为?

However, I can only do that explicitly. If some unexpected run-time error happens, the .vbs quits with an error dialog box, however the exit code is zero so my batch file thinks it suceeded! How can I change that behavior?

如果你正在考虑说,使用上的错误goto语句,不要打扰...这语法是常规VB可用,但不是在VBScript。

And if you are thinking of saying, use on error goto, don't bother... that syntax is available in regular VB, but not in VBScript.

推荐答案

我想一个不折不扣的现成解决方案......谁说0有权意味着成功?有时的VBScript返回一个0返回code代表失败,那么为什么不接受呢?采用0作为(至少一个可能的)失败code和弥补另一个号码(如10)作为成功code。

I thought of an out-of-the-box solution... Who says 0 has to mean success? VBScript sometimes returns a 0 return code for failures, so why not embrace that? Adopt 0 as (at least one possible) failure code and make up another number (e.g. 10) as the "success code".

在脚本的末尾,把WScript.Quit(10)。如果一切成功到这一点,将只被击中。然后,而不是如果错误级别1中调用批处理文件,使用如果%ERRORLEVEL%= = 10

At the end of the script, put WScript.Quit(10). That will only be hit if everything succeeded up to that point. Then instead of "if errorlevel 1" in the calling batch file, use "if %errorlevel% == 10"

这篇关于捕获VBScript中的任何错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆