Jump to content

Error Handling


DuncanM
 Share

Recommended Posts

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.

Link to comment
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?
Link to comment
Share on other sites

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

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

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

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

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...