Handling errors (only visual project type)


Easy Code has a final handler to catch exceptions (errors) fired when calling any function of its visual libraries. This error handler only takes effect when the project was built in Debug mode (see Project properties), and if the dbghelp.dll exists in the system directory. When an exception occurs, the final handler shows a dialog box where you can see the corresponding code and description, and the name of the window or module, plus the line number, which caused the exception.

Anyway, for a better and more precise control, you can handle errors at procedure level (or even at message level) with these two macros:

    StartErrorHandler <Offset HandlerAddress>

    EndErrorHandler


StartErrorHandler
starts the error handling, specifying the address that will receive control when an error occurs, while EndErrorHandler specifies that the error handling ends its control. You can use these macros in the following way:

AnyProcedure Proc
    StartErrorHandler Offset ErrorsPlace
    .........
    code.....
    .........
Exit
:
    EndErrorHandler
    Ret

ErrorsPlace:
    .If Error.Code == 0C0000005H; Access violation read
        do something
    .EndIf
    Jmp
Exit
AnyProcedure EndP

This error handler will be active along all code between the two sentences (StartErrorHandler and EndErrorHandler), and any other procedures called inside it (unless those other procedures have their own error handler), until EndErrorHandler is reached (in the example aboce, at the end of the procedure). It is very important, for a correct behavior of the application, that each StartErrorHandler matches an EndErrorHandler at the same level (usually at the end of the procedure).


You can accurate even more the error handling. For example:

    .If uMsg == WM_CREATE
        StartErrorHandler Offset ErrorsPlace1
        .........
    
    code.....
        .........
        Jmp
Exit1

ErrorsPlace1:
        .If Error.Code == 0C0000005H ; Access violation read
            do something
        .EndIf

Exit1:
        EndErrorHandler

    .ElseIf uMsg == WM_DESTROY
        StartErrorHandler Offset ErrorsPlace2
        .........
        code.....
        .........
        Jmp Exit2

ErrorsPlace2:
        .If Error.Code == 0C0000005H ; Access violation read
            do something
        .EndIf

Exit2:
        EndErrorHandler
    .EndIf

This type of handling works like Push and Pop. So, as said before, you should write an EndErrorHandler for each StartErrorHandler at the same level (take into account whether the procedure has two or more Ret o Return sentences due to the stack frame), so that it removes the handler address from the stack. If you do not do that, next time the program execution enters the procedure for processing any other message and an error occurs, it will not work properly.