Handling exceptions (Only Visual Project Type)


Easy Code can create an error handler for catching exceptions (errors) fired during the program execution, so 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 effective 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.....
    .........
    EndErrorHandler
@@: Ret

ErrorsPlace:
    EndErrorHandler
    .If Error.lCode == 0C0000005H ;Access violation read
        do something
    .EndIf
    Jmp
@B
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 above, 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.....
        .........
        EndErrorHandler
@@:     Ret


ErrorsPlace1:
        EndErrorHandler
        .If Error.lCode == 0C0000005H ;Access violation read
            do something
        .EndIf
        Jmp @B


    .ElseIf uMsg == WM_DESTROY
        StartErrorHandler Offset ErrorsPlace2
        .........
    
    code.....
        .........
        EndErrorHandler
@@:     Ret


ErrorsPlace2:
        EndErrorHandler
        .If Error.lCode == 0C0000005H ;Access violation read
            do something
        .EndIf
        Jmp @B

    .EndIf

This type of handling cannot be nested, 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.

REMARKS: All the code above is written in Masm syntax. You will have to make the necessary modifications for other assemblers.