Sign in to follow this  
Followers 0
karolisk

how to suspend error popup and quietly exit script on error?

22 posts in this topic

#1 ·  Posted (edited)

Hi All,

I'm not so pro in autoit so I do not expect that I will be able to handle all the errors. So if error happens I want script to exit quietly (without popup error message or it to close automatically). (My script is reading some data from database and performing some actions with IE.) (After some time, I will be checking if my script is not running restarting it.)

Please let me know if there is a way to do that, thanks.

Edited by karolisk

Share this post


Link to post
Share on other sites



In AutoIt it's very simple to handle errors. Everyone can write "error free" code.

I'll give you a quick "tutorial" to writing error free code.

If you have a funciton and you think it might fail ( you can also see if it can fail in the help file ) Then put this after the function:

If @error Then Exit

For example:

$hWnd = WinGetHandle("omglol")
If @error Then Exit

If you are working with arrays you need to add a few checks with UBound. But we'll get to that later.

Congratulations, you can now write error free code. And that's all there is to it!

If you have any specific questions, please come back later.

Share this post


Link to post
Share on other sites

i think he is talking about getting error in the application being automated. like an unhandled exception dialog in .NET.

the way i handle it is that i have a function where i have written the window headers for all the usual errors that i get. whenever any of these windows exist, my script takes a screenshot, logs what was the last action done and exits. i just call this function after every button click, control interaction etc.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

thanks for help, but I know how to handle errors with '@error'. I want to know what to do if unhandled error pops up so that my script doesn't get stuck and exit (or even better - restart :D).

Edited by karolisk

Share this post


Link to post
Share on other sites

You can't handle all errors (like COM errors) so simply, just by checking '@error' (I'm using this technique). OK, there are other ways to handle them, but 'what if'. I want to be 100% sure that it doesn't get stuck even if I miss something and something goes wrong.

And one more reason: I'm lazy :D. It would be MUCH easier and faster to write code without so much error handling. (I need to learn MUCH to write a good error proof code and I need my script to work reliably now.)

It would be a nice way to avoid many problems if its possible. So, is it possible?

Share this post


Link to post
Share on other sites

"MUCH easier and faster to write code without so much error handling." I dont think it is possible. That is why debuggers , beta testers and so on exist . You cant write a foolproof software so easily.

Share this post


Link to post
Share on other sites

Maybe I should just run one more simple bot in a background which would just press 'OK' button when error pops up? :D

Share this post


Link to post
Share on other sites

You can't handle all errors (like COM errors) so simply, just by checking '@error' (I'm using this technique). OK, there are other ways to handle them, but 'what if'. I want to be 100% sure that it doesn't get stuck even if I miss something and something goes wrong.

If you want to handle COM errors, you do it like this:

; COM Error Handler example
; ------------------------- 

$oIE=ObjCreate("InternetExplorer.Application.1")    ; Create Internet Explorer application

Global $g_eventerror = 0    ; to be checked to know if com error occurs. Must be reset after handling.

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")    ; Initialize a COM error handler

$oIE.UnknownMethod      ; Deliberately call an undefined method

If $g_eventerror then
    $g_eventerror = 0
  Msgbox (0,"AutoItCOM test","Test passed: We got an error number: " & @error)
Else
  Msgbox (0,"AutoItCOM test","Test failed!")
Endif

Exit

; This is my custom defined error handler
Func MyErrFunc()

  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"      & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
            
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    
    $g_eventerror = $err  ; to check for after this function returns
Endfunc

It would be a nice way to avoid many problems if its possible. So, is it possible?

No, it's not possible because it would be redundant. The best way avoid problems is to write code with error checking in the right places.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I tried to put COM handler already, but I get another error then. So I will have to dig deeper into this... or get back to my plan and try to make another background bot.

No, it's not possible because it would be redundant. The best way avoid problems is to write code with error checking in the right places.

I don't see how hiding error popup is redundant. And yes, I don't argue with you that its best. But at the moment I just need a result - never stucking bot.

Here is text of error popup, I want to suspend, in case someone don't understand about what I'm talking about.

---------------------------

AutoIt Error

---------------------------

Line -1:

Error: The requested action with this object has failed.

---------------------------

OK

---------------------------

Edited by karolisk

Share this post


Link to post
Share on other sites

Did you try the "COM Error Handler example" ?

Post the code where you receive the error.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

well I tried it in a bit different way.

now I see that probably my problem was because I called

$ie = _IECreate ("http://onesurvey.com/Voxco.Web/", 1, 1, 1)

after

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")    ; Initialize a COM error handler

but why??

Edited by karolisk

Share this post


Link to post
Share on other sites

well I tried it in a bit different way.

now I see that probably my problem was because I called

$ie = _IECreate ("http://onesurvey.com/Voxco.Web/", 1, 1, 1)

after

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")    ; Initialize a COM error handler

but why??

It should not make a different.

Share this post


Link to post
Share on other sites

well, if I do this:

#include <IE.au3>

Global $oMyError = ObjEvent("AutoIt.Error","errHandler")
$ie = _IECreate ("http://google.com", 0, 1, 1)
Exit

it calls "errHandler".

Share this post


Link to post
Share on other sites

well, if I do this:

#include <IE.au3>

Global $oMyError = ObjEvent("AutoIt.Error","errHandler")
$ie = _IECreate ("http://google.com", 0, 1, 1)
Exit

it calls "errHandler".

Because an error has happen, I meant to say that it does not make a different as long as AutoIt.Error is before the error.

Share this post


Link to post
Share on other sites

ok, so now why is this error happening?

Share this post


Link to post
Share on other sites

The IE library is extensively error checked, including COM error checked. Any errors from the IE library will not hard crash your application.

Share this post


Link to post
Share on other sites

Try this one :

_IEErrorHandlerRegister("MyErrFunc")
$ie = _IECreate ("http://google.com", 0, 1, 1)
Exit

Func MyErrFunc()
    ;$oError =$$oIEErrorHandler.description
EndFunc   ;==>MyErrFunc

IE has its own error handler.

Share this post


Link to post
Share on other sites

I don't have any problem.

Everything open just fine (Win7 RTM X86)

well, but it does here. Maybe I will look into this problem when my script will be 'error handled' in other way.

Try this one :

_IEErrorHandlerRegister("MyErrFunc")
$ie = _IECreate ("http://google.com", 0, 1, 1)
Exit

Func MyErrFunc()
    ;$oError =$$oIEErrorHandler.description
EndFunc   ;==>MyErrFunc

IE has its own error handler.

I tried this already but didn't feel much effect.

thanks for help all. For now, I will make second script which will be handling error and restarting script.

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