Jump to content

Error handling idea


amokoura
 Share

Recommended Posts

My problem was not having error messages up on a deep, deep function call stack. Using plain @error was problematic.

I quickly made an interface for exception-like error handling.

There isn't any real code or implementations yet, it's just a raw interface I figured on a bus trip.

_errorCreate( $msg ) - Create global error id. Returns error id.

_errorRaiseMsg( $msg ) - Raise general error with custom message.

_errorRaise( $id ) - Raise defined error

_errorCatch( $id ) - Catch error. If id is null, catch any error. Returns boolean whether error was caught.

_errorCaughtMsg() - Returns caught error message.

_errorCaughtId() - Returns caught error id.

_errorIs() - Returns boolean whether there's an error.

Example use:

; Create custom error id
Global $ERR_SOMEBAD = _errorCreate( "Something bad happened!" )

main()

func main()
    
    foo()
    
    ; Expect something behind foo() to raise an error
    if _errorCatch($ERR_SOMEBAD) Then
        MsgBox(0,"Specific error!", _errorCaughtMsg())
    ElseIf _errorCatch()
        MsgBox(0,"Unknow error!", _errorCaughtMsg())
    Else
        ; No error
    EndIf
EndFunc

func foo()
    badFunction()
    if _errorIs() then Return
    
    ; this will not be executed because badFunction() caused error
    goodFunction()
EndFunc

func badFunction()
    _errorRaise($ERR_SOMEBAD)
    Return

    ; Would also work:
    ;_errorRaiseMsg( "this is a general error")
EndFunc

After a catch the error is erased so that no other function can catch it anymore.

Edited by amokoura
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...