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

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

问题描述

我有一个调用 VBScript (.vbs) 程序的批处理文件.调用它后,我的批处理脚本检查 %errorlevel% 以查看 .vbs 程序是否失败.我可以使用 WScript.Quit(1) 在 .vbs 程序中用退出代码表示失败.

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 会退出并显示错误对话框,但是退出代码为零所以我的批处理文件认为它成功了!我该如何改变这种行为?

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?

如果您想说,使用 on error 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 返回码,那么为什么不接受呢?采用 0 作为(至少一个可能的)失败代码,并组成另一个数字(例如 10)作为成功代码".

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).只有到那时一切都成功了,才会受到打击.然后在调用批处理文件中使用if %errorlevel% == 10"代替if errorlevel 1"

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天全站免登陆