Sign in to follow this  
Followers 0
DuncanM

Error Handling

17 posts in this topic

I'm in the process of writing a script to automate the browser-based configuration of a network device. As part of this, there are a lot of object references to forms/buttons, button clicks, etc. So, my question is, what is the best way to implement error handling? I'm eventually going to be turning this over to end users who have no AutoIT experience (as well, it will be distributed as an .exe with no tray icon), so I need to make that it displays adequate feedback so that I can troubleshoot in the event that there is a failure. Thanks in advance.

Share this post


Link to post
Share on other sites



Maybe...

_IEErrorHandlerRegister()

see help

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Maybe...

_IEErrorHandlerRegister()

see help

8)

Thanks - maybe I'm missing something with _IEErrorHandlerRegister(), but I'm looking for something that's not so cryptic so that when the end user calls me, they will be able to read an error from (for example) a popup window. Basically, I just want to know where the script is stopping, and why. Will this require adding checks to be written for every WinWait, object creation, etc?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks - maybe I'm missing something with _IEErrorHandlerRegister(), but I'm looking for something that's not so cryptic so that when the end user calls me, they will be able to read an error from (for example) a popup window. Basically, I just want to know where the script is stopping, and why. Will this require adding checks to be written for every WinWait, object creation, etc?

i understand you are working with "object references" the _IEErrorHandlerRegister ("MyErrFunc") below will handle the object error and a message box will display the errors encountered..

MsgBox(0,"COM Error", $ErrorOutput) in the script below

#include <IE.au3>
; Register a customer error handler
_IEErrorHandlerRegister ("MyErrFunc")
; Do something

Exit

Func MyErrFunc()
    ; Important: the error object variable MUST be named $oIEErrorHandler
    $ErrorScriptline = $oIEErrorHandler.scriptline
    $ErrorNumber = $oIEErrorHandler.number
    $ErrorNumberHex = Hex($oIEErrorHandler.number, 8)
    $ErrorDescription = StringStripWS($oIEErrorHandler.description, 2)
    $ErrorWinDescription = StringStripWS($oIEErrorHandler.WinDescription, 2)
    $ErrorSource = $oIEErrorHandler.Source
    $ErrorHelpFile = $oIEErrorHandler.HelpFile
    $ErrorHelpContext = $oIEErrorHandler.HelpContext
    $ErrorLastDllError = $oIEErrorHandler.LastDllError
    $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0,"COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc  ;==>MyErrFunc

i think this will do what you are asking

Will this require adding checks to be written for every WinWait, object creation, etc?

No, it only needs registered once

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I will give it a try - thanks for all your help!

Edited by DuncanM

Share this post


Link to post
Share on other sites

Ok, I'm testing it, but I haven't been able to get the error popup to appear when it's waiting for a window that's not there. I figured it would come up after the timeout, but so far nothing - any ideas?

Share this post


Link to post
Share on other sites

I have the following code thus far:

#include <IE.au3>
; Register a customer error handler
_IEErrorHandlerRegister ("MyErrFunc")
;WinWait should time out, since this window doesn't actually exist
WinWait("sdlkfja", "sdfljk")

Exit

Func MyErrFunc()
   ; Important: the error object variable MUST be named $oIEErrorHandler
    $ErrorScriptline = $oIEErrorHandler.scriptline
    $ErrorNumber = $oIEErrorHandler.number
    $ErrorNumberHex = Hex($oIEErrorHandler.number, 8)
    $ErrorDescription = StringStripWS($oIEErrorHandler.description, 2)
    $ErrorWinDescription = StringStripWS($oIEErrorHandler.WinDescription, 2)
    $ErrorSource = $oIEErrorHandler.Source
    $ErrorHelpFile = $oIEErrorHandler.HelpFile
    $ErrorHelpContext = $oIEErrorHandler.HelpContext
    $ErrorLastDllError = $oIEErrorHandler.LastDllError
    $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0,"COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc ;==>MyErrFunc

Now what I want it to do is, when the WinWait statement times out (because the window doesn't exist), have the error message popup appear. However, it is not doing this. What am I missing?

Share this post


Link to post
Share on other sites

Can someone point out what I'm doing wrong from my code?

Share this post


Link to post
Share on other sites

WinWait does not error...

This is a strange concern as we have gone from a COM error to an AutoIt error?

Please look at this example to check for a window.

If Not WinWait('Title', '', 10) Then
    MsgBox(0x40000, 'Title', 'Houston...We have a problem')
EndIf

Share this post


Link to post
Share on other sites

WinWait does not error...

This is a strange concern as we have gone from a COM error to an AutoIt error?

Please look at this example to check for a window.

If Not WinWait('Title', '', 10) Then
    MsgBox(0x40000, 'Title', 'Houston...We have a problem')
EndIf
MHz, that is what I was asking initially - basically, do I have to write individual checks for (almost) each statement (i.e. IF, ELSE), or is there a way to write a "blanket" function? From what you're saying, I'm thinking it's going to be the former of the 2, since WinWait doesn't timeout - correct?

Share this post


Link to post
Share on other sites

WinWait does timeout. Maybe confusing how I stated error as being the @error macro as WinWait uses the return value instead of failure which you can boolean with checking directly or using Not for failure.

In example

If Not WinWait('Title', '', 10) Then
    MsgBox(0x40000, 'Title', 'Houston...We have a problem')
EndIf
oÝ÷ Ù«­¢+Ø)%]¥¹]¥Ð ÌäíQ¥Ñ±Ìäì°ÌäìÌäì°ÄÀ¤Q¡¸(%5Í   ½à ÁàÐÀÀÀÀ°ÌäíQ¥Ñ±Ìäì°Ìäí!½ÕÍѽ¸¸¸¹]¥¥Ð°= å Ìäì¤)¹%(

A return of 0 is false and anything else is true so we boolean the result. The Not keyword makes booleaning easy to understand and read then checking for 0 or anything non-zero.

Share this post


Link to post
Share on other sites

do I have to write individual checks for (almost) each statement (i.e. IF, ELSE)

Sorry, missed some of your query.

For good responses to errors and avoiding debugging nightmare, yes indeed. Writing up to 50% more code does pay off. You just need to be able to type alot for the effect. :whistle:

Share this post


Link to post
Share on other sites

That is exactly what I was asking..I want as much info as possible in the event of an error or pause in the script, so I'll just have to go back and add the If, Else statements. Thanks again! :whistle:

Share this post


Link to post
Share on other sites

Sorry, forgot to ask - can I do the same type of If/Else statements for a ControlClick, object creation, etc (as in, if the click or object creation fails, it returns a 0, otherwise a 1)?

Share this post


Link to post
Share on other sites

Com objects do rely on @error normally. So do check error, the help file will help to inform.

@error is best to check at default if available for the inbuilt function.

$oShell = ObjCreate("shell.application")
If @error Then
    MsgBox(0x40000, 'Object', 'Failed')
EndIf

Share this post


Link to post
Share on other sites

If the previous does not satisfy with COM errors then look close at the ObjEvent function within the help file.

ObjEvent ( "AutoIt.Error" [, "function name"])

especially the example. :whistle:

Share this post


Link to post
Share on other sites

Com objects do rely on @error normally. So do check error, the help file will help to inform.

@error is best to check at default if available for the inbuilt function.

$oShell = ObjCreate("shell.application")
If @error Then
    MsgBox(0x40000, 'Object', 'Failed')
EndIf
You're a genious..thanks!

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