News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Error Handling

Started by Gabbadar, July 05, 2007, 11:24:51 PM

Previous topic - Next topic

Gabbadar

Would anybody have any suggested reading when it comes to error handling, in assembly, or in general? I'd like to put together a function of sorts that creates an error message, and displays them in a uniform, template kind of way, rather than handling every error, and message alert inline. This way I can generically handle unexpected errors, and in the future add functionality to log them to file, database, or dump oodles of info to help me diagnose problems.

I may be thinking of it from a higher-level than I should be. What do you think? Any best-practice suggested reading?

six_L

HandleError proc lpTitle:DWORD
     
LOCAL lpMsgBuffer     : LPVOID ;dword
; calculate language ID, asm version of MAKELANGID
mov cx, SUBLANG_DEFAULT
shl ecx, 10
;or  cx, LANG_NEUTRAL        ; LANG_NEUTRAL = 0, nothing necessary

; Setup parameters for FormatMessage, normal pushing to use some
; params directly (e.g. GetLastError returns the ID in eax, but I
; can't use this register in "invoke")

push NULL                ; we don't need this
push 0                   ; min. size of output buffer if we use
; FORMAT_MESSAGE_ALLOCATE_BUFFER
lea  ebx,lpMsgBuffer     ; get address of our buffer
push ebx                 ; address of buffer
push ecx                 ; our language ID, calculated above
invoke GetLastError      ; get error number
push eax                 ; push return value = error ID
push NULL                ; can be used to format a string, we don't need it
mov edx, FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM
push edx                 ; some flags, check your doc for more
call FormatMessage       ; here we go

; Display error-message
invoke MessageBox, NULL, lpMsgBuffer, lpTitle, MB_OK or MB_ICONSTOP
; free memory
invoke LocalFree, lpMsgBuffer

ret
HandleError endp


for example:

invoke CreateFile,addr szfileName,GENERIC_READ or GENERIC_WRITE,0,NULL,OPEN_EXISTING,\
     FILE_ATTRIBUTE_NORMAL,NULL
.if eax==INVALID_HANDLE_VALUE
invoke HandleError,addr pzErrorCaption
mov eax,FALSE
regards

Tedd

Well there are two (main) approaches to handling errors: returning error codes (or setting an error 'flag'), and exceptions.
Error codes is something we're all probably familiar with, but read up on how exceptions work and the difference between 'catching' and 'throwing.'

As for a template, maybe something like: "<type> <code> <description>"
where, <type> is some kind of error 'class' (warning/error/fatal/exception, or however you decide to split it up);
<code> is an error code (ascii digits), useful because it's (human) language independent and easily machine readable - in many cases it could be identical to the windows error code;
<description> is a human readable description of the error, in the native language of the machine (i.e. english/french/spanish/.....) - for windows error codes you can get this text from the FormatMessage function.

'type' might not be necessary, as you could absorb it into the error-code, but I think it provides an extra categorisation without 'spoiling' the original error code.
No snowflake in an avalanche feels responsible.