如何实现错误处理?
How to implement error handling?
我的 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
超过该文件夹中的文件数时,将发生错误。我试过了但没用(来自 "HELP SECTION" 和论坛 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
尝试实施错误检查。
If Not FileExists(your string with the $n) Then
... abort
Else
shellexecute ...
您可以改用 _FileListToArray()
。
我推荐第二个例子,因为它首先防止了错误。但是,第一个示例可以用作通用错误检查器。
示例 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
An error will occur when $n
exceeds the number of files in that folder.
根据Documentation - Function Reference - _FileListToArray()
:
Lists files and\or folders in a specified folder (Similar to using Dir with the /B Switch)
将只循环现有的文件名(完全避免错误)。示例:
#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()
。
.
我的 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
超过该文件夹中的文件数时,将发生错误。我试过了但没用(来自 "HELP SECTION" 和论坛 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
尝试实施错误检查。
If Not FileExists(your string with the $n) Then
... abort
Else
shellexecute ...
您可以改用 _FileListToArray()
。
我推荐第二个例子,因为它首先防止了错误。但是,第一个示例可以用作通用错误检查器。
示例 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
An error will occur when
$n
exceeds the number of files in that folder.
根据Documentation - Function Reference - _FileListToArray()
:
Lists files and\or folders in a specified folder (Similar to using Dir with the /B Switch)
将只循环现有的文件名(完全避免错误)。示例:
#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()
。