【问题标题】:How to implement error handling?如何实现错误处理?
【发布时间】:2016-03-03 21:32:24
【问题描述】:

我的 AutoIt 脚本生成了一个我想要处理的错误。任何错误都转到自定义函数的方法也可以。在 VBA 中,我使用 OnErrorGoTo,但在 AutoIt 中找不到类似的东西。

我的代码:

Func Start()
    While 1
        If ProcessExists ( "Photoshop.exe" ) <> 0 Then
            Sleep(5000)
        Else
            Local $sFile ="C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"
            Local $iPID = ShellExecute($sFile)
            Sleep(10000)
            $n = $n+1
        EndIf
    WEnd
EndFunc

$n 超过该文件夹中的文件数时会发生错误。我试过了,但没有用(来自“帮助部分”和论坛帖子):

Global $iEventError = 0 ; To be checked to know if COM error occurs. Must be reset after handling.
Local $oMyError = ObjEvent("AutoIt.Error", "ErrFunc") ; Install a custom error handler

Func Start()
    While 1
        If ProcessExists ( "Photoshop.exe" ) <> 0 Then
            Sleep(5000)
        Else
            Local $sFile ="C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"
            Local $iPID = ShellExecute($sFile)
            If $iEventError Then
                MsgBox($MB_OK, "", "There was an error on the previous line.")
                $iEventError = 0 ; Reset after displaying a COM Error occurred
            EndIf
            Sleep(10000)
            $n = $n+1
        EndIf
    WEnd
EndFunc

; This is my custom error handler 
Func MyErrFunc() 
    Msgbox(0,"","ERROR GENERATED ON " & $n)
Endfunc

【问题讨论】:

标签: error-handling autoit


【解决方案1】:

我推荐第二个示例,因为它首先防止了错误。但是,第一个示例可以用作一般的错误检查器。

示例 1

Start()

Func Start()

    Local $n = 1
    While 1
        If ProcessExists("Photoshop.exe") <> 0 Then
            Sleep(5000)
        Else
            Local $sFile = "C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"

            Local $iPID = ShellExecute($sFile)
            If @error Then MyErrFunc(@ScriptLineNumber, @error) ;check for error

            Sleep(10000)
            $n = $n + 1
        EndIf
    WEnd
EndFunc   ;==>Start

; error handler
Func MyErrFunc($iLineNumer, $iError)
    $iLineNumer = $iLineNumer - 1
    MsgBox(0, "", "ERROR GENERATED ON SCRIPT LINE: " & $iLineNumer & @CRLF & "ERROR CODE: " & $iError)
EndFunc   ;==>MyErrFunc

示例 2

Start2()

Func Start2()

    Local $n = 1
    While 1
        If ProcessExists("Photoshop.exe") <> 0 Then
            Sleep(5000)
        Else
            Local $sFile = "C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"

            If FileExists($sFile) Then
                Local $iPID = ShellExecute($sFile)
                Sleep(10000)
            Else ;handle error (you could use a function here if you wanted)
                ConsoleWrite("File not found: " & $sFile & @CRLF)
            EndIf

            $n = $n + 1
        EndIf
    WEnd
EndFunc   ;==>Start2

【讨论】:

    【解决方案2】:

    尝试实施错误检查。

    If Not FileExists(your string with the $n) Then
        ... abort
    Else
        shellexecute ...
    

    您可以改用_FileListToArray()

    【讨论】:

    • 我想你不需要为此使用 COMError 处理程序。使用不存在的文件调用 shellexecute 时会发生错误。这就是为什么我建议在调用 shellexecute 之前检查文件是否存在。就是这样。
    【解决方案3】:

    $n 超过该文件夹中的文件数时会发生错误。

    根据Documentation - Function Reference - _FileListToArray()

    列出指定文件夹中的文件和\或文件夹(类似于将 Dir 与 /B 开关一起使用)

    将仅循环现有文件名(完全避免错误)。示例:

    #include <File.au3>
    
    Global Const $g_sPathFiles = 'C:\Auto\CodeToBe\Batch\'
    Global Const $g_sMaskFile  = '*.jsxbin'
    Global Const $g_aFile      = _FileListToArray($g_sPathFiles, $g_sMaskFile, $FLTA_FILES, True)
    
    For $i1 = 1 To $g_aFile[0]
    
        ShellExecuteWait($g_aFile[$i1])
    
    Next
    

    也可以使用_FileListToArrayRec()

    Related.

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多