au3如何防止当脚本出现未知错误时弹出窗口
本帖最后由 vivier001 于 2009-10-1 19:55 编辑比如下面这个错误的代码
Dim $n
$n=1
MsgBox(0, "", $n)
当运行时会弹出AutoIt 错误窗口。
怎样让它遇到像上面代码中错误时不弹出AutoIt 错误直接退出脚本呢? 没人知道吗??高手在那里啊 请多多搜索,我问了两次了,没办法不弹,不过也不会全没有,那就是避免错误,只要没有错就没问题办法 Dim $n
$n=1
MsgBox(0, "", $n)
以上错误是最基本的错误,谁都不应该范吧! 呵呵,他只是假设 设置一个func截取错误,然后选择是否弹出窗口 这个问题问的好啊.
异常处理,在很多情况下,必须做的,否则程序崩溃无法控制.
对于数组的溢出.一般可以这样处理
Dim $n
$n=1
if IsArray($n) then
MsgBox(0, "", $n);确定为数组才执行,避免异常.
endif
如果 MsgBox(0, "", $n);同样会出错.
还有个函数可以解决这个问题.
UBound
Return Value
Success: Returns the size of the array dimension.
Failure: Returns 0 and sets @error:
1 = Array given is not an array.
2 = Array dimension is invalid.
if UBound($n) >=5 then
msgbox(0,"",$n) ;判断数组有5个以上成员才对下标进行操作,这样避免溢出.
endif
至于$n=1这样的语句,是不应该出现的.AU3可能可以编译通过,其它语言是不行的. 那么如何避免在ie操作里面的弹出错误呢?比如下面的
#include<ie.au3>
.........................
while 1
$oie=_IECreate("http://www.***.com/")
$ouser = _IEGetObjByName($oie,"name")
$opass = _IEGetObjByName($oie,"password")
_IEFormElementSetValue($ouser,$user)
_IEFormElementSetValue($opass,$pass);;;
..............
wend
...............
当循环执行时,ie窗口不小心被关闭的话有时会弹出来autoit错误,点确定后,然后脚本就退出了。
怎样让他不弹出窗口而继续循环下去呢? 自己解决了,呵呵 我用的是傻瓜方法,另做两个脚本,一个脚本专门捕捉错误窗口,跳出就自动点击确定;另一个自动检查进程,进程关闭了,就自动再打开。
我用ie.au3也经常遇到错误,最容易遇到的错误的是
--> IE.au3 V2.4-0 Error from function _IEPropertyGet, $_IEStatus_InvalidDataType
不知道怎么回事 可以用這個
http://www.autoitscript.com/forum/index.php?showtopic=79950&st=0&p=575904&hl=_error&fromsearch=1&#entry575904;#=#INDEX#==================================================================# ;#Title .........: _Error Handler.au3v 1.2 # ;#Description....: AutoIt3 Error Handler & Debugger # ;#Date ..........: 7.9.08 # ;#Authors .......: jennico (jennicoattminusonlinedotde) # ;# @MrCreatoR # ;# MadExcept (GUI inspiration by mrRevoked) # ;#==========================================================================# #include-once Dim $s_msg,$s_icn,$s_lb1,$s_lb2,$s_old=1 _OnAutoItError() ;#=#Function#===============================================================# ;#Name ..........: _OnAutoItError ( ) # ;#Description....: AutoIt3 Error Handler & Debugger GUI # ;#Parameters.....: (None) # ;#Date ..........: 7.9.08 # ;#Authors .......: jennico (jennicoattminusonlinedotde) # ;# @MrCreatoR # ;# MadExcept (GUI inspiration by mrRevoked) # ;#==========================================================================# ; this function is made to be customized ! Func _OnAutoItError() If StringInStr($CmdLineRaw,"/AutoIt3ExecuteScript") Then Return Opt("TrayIconHide",1) ; run a second instance $iPID=Run(@AutoItExe&' /ErrorStdOut /AutoIt3ExecuteScript "'&@ScriptFullPath&'"',@ScriptDir,0,6) Dim $sErrorMsg,$GUI=GUICreate(@ScriptName,385,90,Default,Default,-2134376448) GUISetBkColor(0xE0DFE2) ;BitOR($WS_CAPTION,$WS_POPUP,$WS_SYSMENU) GUICtrlCreateIcon("user32.dll",103,11,11,32,32) GUICtrlSetBkColor(GUICtrlCreateLabel("",1,1,383,1),0x41689E) GUICtrlSetBkColor(GUICtrlCreateLabel("",1,88,383,1),0x41689E) GUICtrlSetBkColor(GUICtrlCreateLabel("",1,1,1,88),0x41689E) GUICtrlSetBkColor(GUICtrlCreateLabel("",383,1,1,88),0x41689E) GUICtrlSetBkColor(GUICtrlCreateLabel("An error occurred in the application.",52,21,175,15),-2) $s_lb1=GUICtrlCreateLabel("",10,60,110,22) GUICtrlSetBkColor(-1,0xEFEEF2) GUICtrlSetState(-1,128) $s_lb2=GUICtrlCreateLabel(" send bug report",28,64,92,15) GUICtrlSetBkColor(-1,-2) GUICtrlSetCursor(-1,0) $s_icn=GUICtrlCreateIcon("explorer.exe",254,13,63,16,16) GUICtrlSetCursor(-1,0) $s_lb1=GUICtrlCreateLabel("",126,60,114,22) GUICtrlSetBkColor(-1,0x706E63) GUICtrlSetState(-1,128) $s_lb2=GUICtrlCreateLabel(" De-Bug Script",145,64,95,15) If @Compiled Then GUICtrlSetData(-1," show bug report") GUICtrlSetColor(-1,0xFFFFFF) GUICtrlSetBkColor(-1,-2) GUICtrlSetCursor(-1,0) $s_icn=GUICtrlCreateIcon("shell32.dll",-81,129,63,16,16) If @Compiled Then GUICtrlSetImage(-1,"shell32.dll",23) GUICtrlSetCursor(-1,0) $s_lb1=GUICtrlCreateLabel("",246,8,131,22) GUICtrlSetBkColor(-1,0xEFEEF2) GUICtrlSetState(-1,128) $s_lb2=GUICtrlCreateLabel(" continue application",265,12,115,15) GUICtrlSetBkColor(-1,-2) GUICtrlSetCursor(-1,0) $s_icn=GUICtrlCreateIcon("shell32.dll",290,249,11,16,16) GUICtrlSetCursor(-1,0) $s_lb1=GUICtrlCreateLabel("",246,34,131,22) GUICtrlSetBkColor(-1,0xEFEEF2) GUICtrlSetState(-1,128) $s_lb2=GUICtrlCreateLabel(" restart application",265,38,115,15) GUICtrlSetBkColor(-1,-2) GUICtrlSetCursor(-1,0) $s_icn=GUICtrlCreateIcon("shell32.dll",255,249,37,16,16) GUICtrlSetCursor(-1,0) $s_lb1=GUICtrlCreateLabel("",246,60,131,22) GUICtrlSetBkColor(-1,0xEFEEF2) GUICtrlSetState(-1,128) $s_lb2=GUICtrlCreateLabel(" close application",265,64,115,15) GUICtrlSetBkColor(-1,-2) GUICtrlSetCursor(-1,0) $s_icn=GUICtrlCreateIcon("shell32.dll",240,249,63,16,16) GUICtrlSetCursor(-1,0) ProcessWait($iPID) While 1 ; trap the error message $sErrorMsg&=StdoutRead($iPID) If @error Then ExitLoop Sleep(1) WEnd If $sErrorMsg="" Then Exit GUISetState() Opt("TrayIconHide",0) Opt("TrayAutoPause",0) WinSetOnTop(@ScriptName,"",1) SoundPlay(@WindowsDir&"\Media\chord.wav") TraySetToolTip(@ScriptName&@CRLF&"An error occurred in the application.") Do; choose action to be taken Dim $s_msg=GUIGetMsg(),$mse=GUIGetCursorInfo($GUI) If @error=0 Then For $i=0 To 4 If $i<>$s_old And ($mse=$s_lb1[$i] Or $mse=$s_lb2[$i] Or _ $mse=$s_icn[$i]) Then __Select($i) Next EndIf If WinActive($GUI) And $s_msg=0 Then HotKeySet("{ENTER}","__Hotkey") HotKeySet("{RIGHT}","__Hotkey") HotKeySet("{LEFT}","__Hotkey") HotKeySet("{DOWN}","__Hotkey") HotKeySet("{TAB}","__Hotkey") HotKeySet("{UP}","__Hotkey") Else HotKeySet("{ENTER}") HotKeySet("{RIGHT}") HotKeySet("{LEFT}") HotKeySet("{DOWN}") HotKeySet("{TAB}") HotKeySet("{UP}") EndIf If $s_msg=$s_lb2 Or $s_msg=$s_icn Then MsgBox(270400,"Continue Application", _ "I am afraid, not possible with AutoIt ! "&@CRLF&@CRLF& _ "( No GoTo command ) :-( ") If $s_msg=$s_lb2 Or $s_msg=$s_icn Then MsgBox(270400,"Send Bug Report", _ "Email successfully sent ! ") ;#include <INet.au3> ; use email ;_INetSmtpMail ( $s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress [,$s_Subject&_ ;[,$as_Body [,$s_helo, [,$s_first [,$b_trace]]]]]) If $s_msg=$s_lb2 Or $s_msg=$s_icn Then If @Compiled=0 Then __Debug($sErrorMsg) If @Compiled Then MsgBox(270400,"Show Bug Report",$sErrorMsg&" ") EndIf Until $s_msg=-3 Or $s_msg=$s_lb2 Or $s_msg=$s_icn Or $s_msg=$s_lb2 Or $s_msg=$s_icn If $s_msg=$s_lb2 Or $s_msg=$s_icn Then Run(@AutoItExe&' "'&@ScriptFullPath& _ '"',@ScriptDir,0,6) SoundPlay(@WindowsDir&"\Media\start.wav") Exit EndFunc ;#=#Function#===============================================================# ;#Name ..........: __Debug ( $txt ) # ;#Description....: Debug Function for _ErrorHandler.au3 # ;#Parameters.....: $txt = Error Message Text from StdoutRead # ;#Date ..........: 7.9.08 # ;#Authors .......: jennico (jennicoattminusonlinedotde) # ;#==========================================================================# Func __Debug($txt) WinSetState(@ScriptName,"",@SW_HIDE) $a=StringSplit($txt,@CRLF,1) Dim $b=StringSplit($a,") : ==> ",1),$number=StringMid($b,StringInStr($b,"(")+1) Dim $code="Error Code: "&@TAB&StringTrimRight($b,2),$line="Line: "&@TAB&$number&" => "&$a Dim $file="File: "&@TAB&StringReplace($b," ("&$number,""),$count=StringLen($code),$height=180 If StringLen($file)>$count Then $count=StringLen($file) If StringLen($line)>$count Then $count=StringLen($line) If StringLen($a)>$count Then $count=StringLen($a) If $count*6>@DesktopWidth-50 Then Dim $count=(@DesktopWidth-50)/6,$height=240 Run(RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\SciTE.exe","")& _ ' "'&@ScriptFullPath&'" /goto:'&$number&","&StringLen($a)-1) $x=InputBox(" Please Correct this line:",$code&@CRLF&@CRLF&$file&@CRLF&@CRLF& _ $line,StringTrimRight($a,1),"",$count*6,$height) WinSetState(@ScriptName,"",@SW_SHOW) If $x="" Or $x=StringTrimRight($a,1) Then Return $t=StringSplit(FileRead(@ScriptFullPath),@CRLF,1) $t[$number]=StringReplace($t[$number],StringTrimRight($a,1),$x) $open=FileOpen(@ScriptFullPath,2) For $i=1 to $t FileWriteLine($open,$t[$i]) Next FileClose($open) ControlSend(@ScriptDir,"","ToolbarWindow32","^R") EndFunc ;#=#Function#===============================================================# ;#Name ..........: __Select ( $i ) # ;#Description....: Select Function for _ErrorHandler.au3 # ;#Parameters.....: $i = Element from Mouse Hover ID # ;#Date ..........: 7.9.08 # ;#Authors .......: jennico (jennicoattminusonlinedotde) # ;#==========================================================================# Func __Select($i) GUICtrlSetBkColor($s_lb1[$i],0x706E63) GUICtrlSetColor($s_lb2[$i],0xFFFFFF) GUICtrlSetBkColor($s_lb1[$s_old],0xEFEEF2) GUICtrlSetColor($s_lb2[$s_old],0) $s_old=$i EndFunc ;#=#Function#===============================================================# ;#Name ..........: __Hotkey ( ) # ;#Description....: Hotkey Functions for _ErrorHandler.au3 # ;#Parameters.....: None # ;#Date ..........: 7.9.08 # ;#Authors .......: jennico (jennicoattminusonlinedotde) # ;#==========================================================================# Func __Hotkey() If @HotKeyPressed="{DOWN}" And $s_old>1 And $s_old<4 Then __Select($s_old+1) If @HotKeyPressed="{RIGHT}" And $s_old<2 Then __Select(1+3*($s_old=1)) If @HotKeyPressed="{TAB}" Then __Select(($s_old+1)*($s_old<4)) If @HotKeyPressed="{LEFT}" And $s_old Then __Select($s_old>1) If @HotKeyPressed="{UP}" And $s_old>2 Then __Select($s_old-1) If @HotKeyPressed="{ENTER}" Then $s_msg=$s_icn[$s_old] EndFunc 我用了多线程来判断,嘿嘿
页:
[1]