Search code examples
error-handlingautoit

How to implement error handling?


My AutoIt script generates an error that I want to handle. A way that any error goes to a custom function will also do. In VBA I use OnErrorGoTo, but I am unable to find something similar in AutoIt.

My Code :

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

An error will occur when $n exceeds the number of files in that folder. I tried this but didn't work (from the "HELP SECTION" and a forum post):

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

Solution

  • I recommend the second example because it prevents an error in the first place. However, the first example can be used as a general error checker.

    Example 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
    

    Example 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