Jump to content

Try Catch Block?


Recommended Posts

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.

Link to comment
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.

Link to comment
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.

Link to comment
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 :)
Link to comment
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

Link to comment
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

Link to comment
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?

Link to comment
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.

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
Link to comment
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. =/

Link to comment
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...
Link to comment
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.

Link to comment
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

Link to comment
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. =)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...