如何实现错误处理?

时间:2015-11-30 09:33:34

标签: error-handling autoit

我的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超过该文件夹中的文件数时,将发生错误。我试过这个但是没有工作(来自&#34; HELP SECTION&#34;和一个论坛帖子):

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

3 个答案:

答案 0 :(得分:4)

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

示例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

答案 1 :(得分:2)

尝试实施错误检查。

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

您可以改用_FileListToArray()

答案 2 :(得分:-1)

  

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

根据Documentation - Function Reference - _FileListToArray()

  

列出指定文件夹中的文件和/或文件夹(类似于在/ B开关中使用Dir)

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

#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