Sign in to follow this  
Followers 0
Zinthose

Simple COM Error Handler

2 posts in this topic

#1 ·  Posted (edited)

My roots are from scripting in VBScript so I miss the "onerror Resume Next" approach to error handling. I believe I have recaptured this feature with a bit of AutoIt flare. :)

I'd say one of the primary issues with the VBScript "onerror Resume Next" function is the cleanup. Some coders "forget" to use "onerror Goto 0" to return to normal error handling. This WILL lead to bugs! To address this, I implemented the error handler to self cleanup after an error check is performed.

The result is far more elegant than I had hoped and thus far it has worked perfectly for by own projects. :)

ComErrExample.au3

#Region - Example
    #Include 'ComErr.au3'
    Dim $Return
    
    _AllExamples()
    
    Func _AllExamples()
        _Example_InvalidObject()
        _Example_InvalidMethod()
        _Example_ReturnFail()
        
        ConsoleWrite( _
            StringFormat(">tExtended code: %sn>tError code: %sn", @extended, @error))

    EndFunc
    
    ;## Example to show failure by invalid object creation
        Func _Example_InvalidObject()
            ConsoleWrite("! _Example_InvalidObject()" & @CRLF)
            
            _onerror()
            Dim $SomeObject = ObjCreate("NoneExistant.Object")
            
            If _Error() Then _
                ConsoleWrite( _
                    StringFormat(">tWinDescription=%sn", _LastComError("WinDescription")))
        EndFunc
                
    ;## Example to show failure by invalid object use
        Func _Example_InvalidMethod()
            ConsoleWrite("! _Example_InvalidMethod()" & @CRLF)
            
            _onerror()
            Dim $SomeObject = ObjCreate("ADODB.RecordSet")
            
            If _Error() Then _
                ConsoleWrite( _
                    StringFormat(">tWinDescription=%sn", _LastComError("WinDescription")))
            
            _onerror()
            $SomeObject.InvalidMethod("ERROR")

            If _Error() Then _
                ConsoleWrite( _
                    StringFormat(">tWinDescription=%sn", _LastComError("WinDescription")))
        EndFunc
    
    ;## Example to show immediate function failure and error return.
        Func _Example_ReturnFail()
            ConsoleWrite("! _Example_ReturnFail()" & @CRLF)
            
            _onerror()
            Dim $SomeObject = ObjCreate("NoneExistant.Object")
            If _Error() Then Return SetError(1, @error, -1)
        EndFunc
#EndRegion
Edited by Zinthose

--- TTFN

Share this post


Link to post
Share on other sites



Amazing, Very good code, I will try it will my projects

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