Jump to content
Sign in to follow this  

Ignore error

Recommended Posts



I'm sure this question has already been answered but doing a search for "ignore errors" brings up too many results. Is there an option similar to VBscript's On error resume next?

I have some code that will change the password for a list of local accounts on the machine. I already catch the error if the account doesn't exist. Most of the time the person running the script will have access to change the passwords but when they don't the code will generate an error. I want to just ignore the error.



Here is the code, it takes an array of usernames and opens a WinNT Object to change the password. I already catch the error if the user doesn't exist.:

For $UserName IN $UserNameArray

$objUser = ObjGet("WinNT://" & @ComputerName & "/" & $UserName & ",user")

If @error = 0 Then





Share this post

Link to post
Share on other sites

COM Error Handling

Using COM without proper error handling can be very tricky. Especially when you are not familiar with the Objects in your script.

An AutoIt script will immediately stop execution when it detects a COM error. This is the default and also the safest setting. In this case you have to take measures in your script to prevent the error from happening.

Only if there is no way to prevent a COM error, you could install an "Error Handler" in which you take action after the error has happened. It is not a solution to make a buggy script work properly. Neither does it catch non-COM related script errors (e.g. declaration and syntax errors).

Error handling is implemented in the same way as a normal COM Event, using ObjEvent() and a user defined COM Event Function. The only difference is the usage of the fixed string "AutoIt.Error" as the name of the object.

An example:

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

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

; Performing a deliberate failure here (object does not exist)

$oIE = ObjCreate("InternetExplorer.Application")

$oIE.visible = 1


if $g_eventerror then Msgbox(0,"","the previous line got an error.")


; This is my custom error handler

Func MyErrFunc()


Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _

"Number is: " & $HexNumber & @CRLF & _

"Windescription is: " & $oMyError.windescription )

$g_eventerror = 1 ; something to check for when this function returns


One thing is special about the Error Event Handler, and that is the Object it returns. This is an AutoIt Error Object that contains some useful properties and methods. It's implementation is partly based on the "Err" Object in VB(script):

Properties of the AutoIt Error Object:

.number The Windows HRESULT value from a COM call .windescription The FormatWinError() text derived from .number .source Name of the Object generating the error (contents from ExcepInfo.source) .description Source Object's description of the error (contents from ExcepInfo.description) .helpfile Source Object's helpfile for the error (contents from ExcepInfo.helpfile) .helpcontext Source Object's helpfile context id number (contents from ExcepInfo.helpcontext) .lastdllerror The number returned from GetLastError() .scriptline The script line on which the error was generated

Methods of the AutoIt Error Object:

.raise Raises an error event, initiated by the user .clear Clears the contents of the Error Object (i.e. numbers to 0, strings to "")

A note for UDF writers

You can only have ONE Error Event Handler active per AutoIt script. If you are writing UDF's containing COM functions, you can check if the user has an Error Handler installed as follows:

$sFuncName = ObjEvent("AutoIt.Error")

if $sFuncName <> "" then Msgbox (0,"Test","User has installed Error Handler function: " & $sFuncName)

If no Error Handler was active, you can temporarily install your own during the UDF call.

However, you can never stop an existing Error Handler without releasing the variable it had been assigned to. If the script author had installed a COM Error Handler, it's his responsibility to use a proper function that will also be able to catch COM errors generated by UDF's.

Just search help...


Edited by Valuater


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