Jump to content

Error Handling


Recommended Posts

I use AutoIt extensively in my Windows deployment solution, including running in Windows XP SP2/SP3, WinPE 3.0, and Windows 7. Since this is an automated process I want to trap ALL errors and log these and handing the routines via our automation process and NOT to popup any message boxes.

However, I get really inconsistent results when using the MyError(). Intermittently I still get the "AutoItCOM Test" message popup instead of my customer handler. Some times the same EXE may run twice at the same time on the machine, and different EXE may run in rapid succession.

Are there any tips and tricks when trying to implement a global error handler across multiple compiled scripts?

Now I usually have multiple different executables which all have #Include Utils.au3 to register the error handler.

Utils.au3:

;Error Handle function
Global $gError = 0  ; to be checked to know if com error occurs. Must be reset after handling.
Global $gMessage = ""
Global $oError

;Clear any previous error handling events
$oError = ObjEvent("AutoIt.Error")
If $oError = "" Then
    $oError = ObjEvent("AutoIt.Error","GetError") ; Install a custom error handler
Else
    ConsoleWrite("Function already exists " & $oError)
EndIf

Func MyErrorFunc()
    GetError()
EndFunc

; This is my custom error handler 
Func GetError() 
    $HexNumber = Hex($oError.number,8)
    $gMessage = "COM Error !" & @CRLF & "Number: " & $HexNumber & @CRLF & "Description: " & $oError.windescription
    ;reset error value and return message
    $gError = 1
    If $DADDrive = "" Then
        If $Log = "" Then
            MsgBox(0,"COM Error",$gMessage)
        Else
            _FileWriteLog($Log,"COM ERROR: " & $gMessage)
        EndIf
    Else
        _WriteStatus(2,"COM ERROR: " & $gMessage)
    EndIf
    Exit($oError.number)
Endfunc
Link to comment
Share on other sites

I might be wrong but I think that error function will work only if you are dealing with objects.

To explain it better: I think that will work when you are using Word, Excel, IExplorer ... but not when a simple error happens (like trying to read a non-existent registry key, or to find a file that doesn't exist ...)

I'm not sure if it is possible to have an unique error handler (there are some attempts in the "Example Scripts" forum - you may want to search for them) but as I said - I might be wrong.

Good luck, I will be watching the thread :graduated:

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Both cases are involving objects, either using the MSXML object or the ADODB object.

For MSXML I had an 'access denied' when attempting to save to a XML with a 'read only' attribute.

For ADODB I had an error trying to access a field that didn't exist.

While it is possible for me to add some code to check for these issues, or to resolve them upfront, it is worrisome to me that AutoIt is not using my error handler and throwing up a pop-up box instead of letting my deplyment engine detect the error and handle the UX.

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