【问题标题】:What is the code to exit/ stop VBscript from running in the event of a condition not being met?如果条件不满足,退出/停止 VBscript 运行的代码是什么?
【发布时间】:2012-11-21 23:40:14
【问题描述】:

我在谷歌上查过了,没有答案!

首先要做的事情。 WScript.Quit 不起作用!我不知道“WScript”是什么,但它显然与网页的客户端脚本无关。我以前在某个地方看到过这个“WScript”的东西,它只会产生错误(可能已经过时或其他什么)所以请不要建议它......

无论如何...我想做的就是在不满足条件的情况下完全停止脚本。显然我不想要“退出子”,因为如果嵌入了该子,代码将继续运行!

我知道“停止”命令,但我的印象是它仅用于调试。

希望是一个非常简单的问题。

更新和结论:在我结束这个主题之前,我将稍微扩展一下我正在尝试做的事情......

我有一些通过按钮单击启动的主要潜艇。为了做到这一点,我不必编辑每个单独的子,我在每个子中嵌入了一个通用子,进行了初步检查。

初步检查的一部分是在用户输入不正确的情况下停止程序。如果检测到错误,我想从那时起停止所有进展。 “退出子”显然会跳过该初步子的其余部分,而主子将继续执行。

最后,这只是写入错误标志(在主子程序中检查)或在每个主程序中合并错误条件操作的情况。这样你退出主子,问题就解决了。

这不是懒惰——我只是想减少代码量。无论如何,谢谢您的回复。

【问题讨论】:

  • 如果人们在输入 -1 之前先阅读问题,那将是公平的。是的,我已经研究过了——如果答案是可用的,那么我不会在这里吗?
  • 好的 - 我可能有点无知,我没有发现“WScript”是什么(我现在知道它意味着 Windows 脚本),但无论哪种方式它都不起作用。它只会给出一个错误,指出该对象(wscript)不存在。
  • 这可能取决于您的脚本代码实际在做什么。在许多情况下,您可能需要使用“On Error Resume Next”来避免未捕获的错误炸毁您的代码。但是随后您必须通过检查来跟踪每个可能出错的语句,例如“If err.Number 0 Then...” 然后您可以以自己的方式“引发”错误并使用 Wscript.Quit( n)(其中“n”是一个数字)。

标签: vbscript


【解决方案1】:

我发现如果使用 wscript.exe 或 cscript.exe 引擎运行 .vbs/.vbe/.wsf 脚本,WScript 始终可用。当 WScript 不可用时,如果使用不同的引擎运行。例如。在 HTA、网页、VBA 或托管 COM 脚本控件中运行 VBScript。

要退出不是从 wscript.exe 或 cscript.exe 运行的脚本,您可以执行以下操作:

main

Sub main
    ' execute code here

    ' oops a confition is not met:
    If Not condition then Exit Sub

    ' more code to execute if condition was met
End Sub

【讨论】:

  • 我可能一开始就走错了路。并不是说我无法执行我打算做的事情,只是我的印象是一定有一种方法可以阻止脚本完全激活(以防止需要放置“exit sub "在每个单独的原始程序中)。我认为可能只是缺乏编程经验让我认为我可以从一个地方停止一切。我现在刚刚将停止条件写入每个主程序。我可能已经合并了一个错误标志。
【解决方案2】:

你可以这样使用:

Sub MyFunc
    ----------
    My Code
    ----------
End Sub

Function Main
  On Error Resume Next
  MyFunc
  If Err.Number <> 0
  Exit Function
End Function

它会停止执行代码,它发现异常或抛出错误。

【讨论】:

    【解决方案3】:

    只有在 Windows 脚本宿主 (wscript.exe,cscript.exe) 中运行时,Wscript 对象才可用。不是 Internet Explorer(iexplorer.exe、mshta.exe)

    一个简单的方法是声明一个全局变量,在发生错误之前设置为 false,如果发生错误,则该变量将设置为 true,之后任何时候检查该变量,您都可以退出sub\function\for\do\whatever.

    例子:

    Dim ErrorOccured
    On Error Resume Next
    ErrorOccured=False
    
    
    Sub Main()
        If ErrorOccured Then Exit Sub
        'some code
        MsgBox "Main has run"
    End Sub
    
    
    Sub MakeAnError
        If ErrorOccured Then Exit Sub
        'some code
        Err.Raise 2
        If Err Then ErrorOccured=True
    End Sub
    
    
    Function TellMeRandom
        If ErrorOccured Then Exit Function
        'some code
        Randomize
        TellMeRandom =Int((100- 1+ 1) * Rnd + 1)*1
    End Function
    
    
    Function ResetError
        ErrorOccured=False
    End Function
    
    
    
    Call Main                 'Main will run because an error has not occured (ErrorOccured is False)
    
    MsgBox TellMeRandom       'This will return a random number 1-100
    
    Call MakeAnError          'This will set the Global ErrorOccured to true
    
    MsgBox TellMeRandom       'This will be blank because the ErrorOccured prevented it from running
    
    Call Main                 'This will not run because the ErrorOccured prevented it from running
    
    Call ResetError           'This will set the Global ErrorOccured to false
    
    Call Main                 'This will run because ErrorOccured is back to False
    
    MsgBox TellMeRandom       'This will return a random number 1-100 because ErrorOccured is back to false
    

    记住添加If ErrorOccured then Exit Sub 用于子例程或If ErrorOccured then Exit Function 用于函数。

    【讨论】:

      【解决方案4】:

      最简单的方法,我发现的是:WScript.Quit

      【讨论】:

        【解决方案5】:

        您还可以引发错误,例如:Err.Raise 507

        此错误将退出您当前的脚本:"An exception occurred".

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-22
          • 1970-01-01
          • 1970-01-01
          • 2020-11-04
          • 2022-01-25
          • 1970-01-01
          相关资源
          最近更新 更多