好像是VBscript异常,怎么处理?

编程入门 行业动态 更新时间:2024-10-27 00:30:16
本文介绍了好像是VBscript异常,怎么处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试 M$ 站点上有关以编程方式调用 Windows 更新的示例.

I'm trying an example from M$ site regarding calling Windows Update programatically.

'http://msdn.microsoft/en-us/library/aa387102%28VS.85%29.aspx
'http://msdn.microsoft/en-us/library/aa386526%28v=vs.85%29.aspx

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = updateSearcher.Search("IsInstalled=1 and Type='Software'")

在执行最后一行时,如果您的网络坏了,我会在 CMD 窗口中看到:

On executing the last line, if your network is broken, I'll see on CMD window:

C:\wu-script\wu-install.vbs(9, 1) (null): 0x8024001F

似乎 updateSearcher.Search 抛出异常并且整个脚本退出.如何捕捉这个异常?

It seems like updateSearcher.Search throws an exception and the whole script exits. How to catch this exception?

我对 VBScript 不是很熟悉.请提供快速提示或参考网址.

I'm not very familiar with VBScript. Please provide a quick hint or a reference URL.

推荐答案

你应该使用 On Error 语句来处理 VBScript 错误.

You should use On Error statement to handling VBScript errors.

'...
'...
On Error Resume Next 'enable error handling
Set searchResult = updateSearcher.Search("IsInstalled=1 and Type='Software'")
If Err.Number <> 0 Then
    'right, this is a catch block :/
    WScript.Echo "error!"
    'WScript.Echo Err.Description
    'more : http://msdn.microsoft/en-us/library/a3c123d4(v=vs.85).aspx
End If
On Error Goto 0 'disable error handling

如您所见,在 VBScript 中捕获错误太麻烦了.但是,也可以使用 javascript 及其 try-catch.
基于您提供的脚本的示例 WSF 包.

wu-install.wsf

As you can see, catching error is too troublesome in VBScript. However, could also use javascript and its try-catch.
A sample WSF package based on the script that you gave.

wu-install.wsf

<?xml version="1.0" ?>
<package>
 <job id="Update">
  <script language="JScript">
   <![CDATA[
        //http://msdn.microsoft/en-us/library/aa387102%28VS.85%29.aspx
        //http://msdn.microsoft/en-us/library/aa386526%28v=vs.85%29.aspx
        var updateSession = WScript.CreateObject("Microsoft.Update.Session");
        var updateSearcher = updateSession.CreateupdateSearcher();
        WScript.Echo("Searching for updates...");
        try {
            var searchResult = updateSearcher.Search("IsInstalled=1 and Type='Software'");
        } catch(err){
            WScript.Echo("error!");
            //WScript.Echo(err.message);
            // more : http://msdn.microsoft/en-us/library/dww52sbt(v=vs.85).aspx
        }
   ]]>
  </script>
 </job>
</package>

使用 cmd 运行:

Runing with the cmd:

C:\wu-script>cscript wu-install.wsf

这篇关于好像是VBscript异常,怎么处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 00:07:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:怎么处理   异常   VBscript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!