Sign in to follow this  
Followers 0
patlim4152

Try Catch Block?

16 posts in this topic

Hey guys, Thanks for all your help thus far. I got my program to be doing for the most part, however because of the uncertainties of the internet, sometimes I get "The page cannot be displayed" or some functions in ie.au3 would crash because it failed to retrieve a certain object.

My question is then, is there anyway I can "catch" these errors like a try catch block in C/C++? So that when these errors appear, instead of having the program in an unsteady state, I'll just terminate the autoit program and restart it?

Thanks again.

Share this post


Link to post
Share on other sites



This will work in most cases:

If @error Then; whatever you want to do here

Share this post


Link to post
Share on other sites

Unfortunately this has been discussed before (http://www.autoitscript.com/forum/index.php?showtopic=20040) with no resolution.


“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Share this post


Link to post
Share on other sites

Hiya, Thanks for your reply, the problem is that sometimes there is 'no time' to check @error as an error box will appear saying that the object is not an object.

For example, from ie.au3 code

Line 1:Func _IEFormGetObjByIndex($o_object, $i_index)

Line 2: If IsObj($o_object.document.forms.item ($i_index)) Then

Line 3: SetError(0)

Line 4: Return $o_object.document.forms.item ($i_index)

Line 5: Else

Line 6: SetError(1)

Line 7: Return 0

Line 8: EndIf

Line 9:EndFunc ;==>_IEFormGetObjByIndex

In Line 2, the program breaks and the error message will pop up (msgbox), because either $o_object isn't an object, or $o_object.document isn't an object or $o_object.document.forms isn't an object.

I guess what I'm trying to figure out is the general case to catch all errors so that I can restart the program.

Share this post


Link to post
Share on other sites

Thanks a lot Stumpii. Sucks though. =x

Share this post


Link to post
Share on other sites

Hey guys, Thanks for all your help thus far. I got my program to be doing for the most part, however because of the uncertainties of the internet, sometimes I get "The page cannot be displayed" or some functions in ie.au3 would crash because it failed to retrieve a certain object.

My question is then, is there anyway I can "catch" these errors like a try catch block in C/C++? So that when these errors appear, instead of having the program in an unsteady state, I'll just terminate the autoit program and restart it?

Thanks again.

No try catch block in AutoIt :)

Share this post


Link to post
Share on other sites

No try catch block in AutoIt :)

Hi,

just a stupid question to this topic. Are there are errors which I cannot "catch" in autoit?

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Hey guys, Thanks for all your help thus far. I got my program to be doing for the most part, however because of the uncertainties of the internet, sometimes I get "The page cannot be displayed" or some functions in ie.au3 would crash because it failed to retrieve a certain object.

My question is then, is there anyway I can "catch" these errors like a try catch block in C/C++? So that when these errors appear, instead of having the program in an unsteady state, I'll just terminate the autoit program and restart it?

Thanks again.

To catch COM errors in AutoIt you need to use the special COM error handler - e.g.: $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

See the Obj/Com section of the beta helpful for details.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

To catch COM errors in AutoIt you need to use the special COM error handler - e.g.: $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

See the Obj/Com section of the beta helpful for details.

Dale

Thanks DaleHohm, That did help somewhat as it allows to exit the program without executing the rest of the program. Now an idea on how to 'restart' the program would be to have a second program to check every hour if the first was running, if it wasn't, then execute it. It is kind of 'unclean', was wondering if anyone knows of a 'cleaner' method?

Share this post


Link to post
Share on other sites

i used this

Func MyErrFunc()
; Set @ERROR to 1 and return control to the program following the trapped error
    SetError(1)
EndFunc  ;==>MyErrFunc

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hiya, Thanks for your reply, the problem is that sometimes there is 'no time' to check @error as an error box will appear saying that the object is not an object.

For example, from ie.au3 code

Line 1:Func _IEFormGetObjByIndex($o_object, $i_index)

Line 2: If IsObj($o_object.document.forms.item ($i_index)) Then

Line 3: SetError(0)

Line 4: Return $o_object.document.forms.item ($i_index)

Line 5: Else

Line 6: SetError(1)

Line 7: Return 0

Line 8: EndIf

Line 9:EndFunc ;==>_IEFormGetObjByIndex

In Line 2, the program breaks and the error message will pop up (msgbox), because either $o_object isn't an object, or $o_object.document isn't an object or $o_object.document.forms isn't an object.

I guess what I'm trying to figure out is the general case to catch all errors so that I can restart the program.

that is a com error that can be set to be ignored, i believe. i think i've seen code to ignore that error, and code to trap it, on this forum.

***edit***

blah. sorry, replied before reading the rest of the thread... my bad.

Edited by cameronsdad

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Was thinking something like

Func MyErrFunc()

; Set @ERROR to 1 and return control to the program following the trapped error

Exit

Restart

EndFunc ;==>MyErrFunc

Checking @error after every object reference is gonna be just so painful. =/

Share this post


Link to post
Share on other sites

Was thinking something like

Func MyErrFunc()

; Set @ERROR to 1 and return control to the program following the trapped error

Exit

Restart

EndFunc ;==>MyErrFunc

Checking @error after every object reference is gonna be just so painful. =/

why? it can be done with a single line, rather than using atleast 2 lines like you would in a Try... Catch...

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

why? it can be done with a single line, rather than using atleast 2 lines like you would in a Try... Catch...

Because there is more than one line with reference to objects, restarting the entire program "sounds" easier than doing error checking after each reference to an object. The function returns the program to the nextline if I am not wrong, and I don't want to execute what is after that if the object reference failed.

Share this post


Link to post
Share on other sites

; Restart your program
; Author UP_NORTH

Func _restart()
    If @Compiled = 1 Then
        Run( FileGetShortName(@ScriptFullPath))
    Else
        Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    EndIf
    Exit
EndFunc

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

; Restart your program
; Author UP_NORTH

Func _restart()
    If @Compiled = 1 Then
        Run( FileGetShortName(@ScriptFullPath))
    Else
        Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    EndIf
    Exit
EndFunc

8)

OMG that rocks. =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0