Handling errors (only visual project type)
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. Yo can use these macros in the following way:
AnyProcedure Proc
StartErrorHandler (Offset ErrorsPlace)
code.....
Exit:
EndErrorHandler
Ret
ErrorsPlace:
Cmp D[Error.Code],
0C0000005H ; Access violation read
Jne <
Exit
do something
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:
Cmp D[uMsg],
WM_CREATE
Jne > L2
StartErrorHandler (Offset ErrorsPlace1)
code.....
Jmp > Exit1
ErrorsPlace1:
Cmp
D[Error.Code], 0C0000005H ; Access violation read
Jne > Exit1
do something
Exit1:
EndErrorHandler
Jmp > End
L2:
Cmp
D[uMsg],
WM_DESTROY
Jne >
Exit2
StartErrorHandler
(Offset ErrorsPlace2)
code.....
Jmp > Exit2
ErrorsPlace2:
Cmp D[Error.Code], 0C0000005H ; Access violation read
Jne >
Exit2
do something
Exit2:
EndErrorHandler
End:
Return (TRUE)
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.