Errorhandler with parsing the errorcode
Include it in your apps for dealing with fatal errors your own way.
Code: Select all
; ---------------------------------------------
; Examplecode for Error-Trap
; Catch Fatal Errors and handle it on your own
; Created by FAKEFACTORY
;
; Purebasic / WIN32
; ---------------------------------------------
; ---------------------------------------------
; Needed structures and constants
; ---------------------------------------------
Structure _EXCEPTION_POINTERS
*ExceptionRecord.EXCEPTION_RECORD;
*ContextRecord.CONTEXT;
EndStructure
#EXCEPTION_ILLEGAL_INSTRUCTION = $C000001D
#EXCEPTION_STACK_OVERFLOW = $C00000FD
; ---------------------------------------------
; Cleanup Procedure
; ---------------------------------------------
Procedure _Cleanup()
Delay(1)
; Here your code for cleanup your allocated memory, bitmaps etc...
EndProcedure
; ---------------------------------------------
; The core of our Error-Trap
; ---------------------------------------------
Procedure _MyExceptionFilter(*ExceptionInfo._EXCEPTION_POINTERS)
If TerminateInProcess.b = #TRUE
; If our own ExceptionFilter fires an error, we need to call the
; OS-Error-Handler for exiting
Procedure_Result.l = SetUnhandledExceptionFilter_(0)
RaiseException_ (0,0,0,0)
End
Else
; Get some info about the error
TerminateInProcess.b = #TRUE
ErrorCode.l = *ExceptionInfo\ExceptionRecord\ExceptionCode
Select ErrorCode.l
Case #EXCEPTION_FLT_DIVIDE_BY_ZERO
Fehler$ = "Error: DIVISION BY ZERO"
Case #EXCEPTION_INT_DIVIDE_BY_ZERO
Fehler$ = "Error: DIVISION BY ZERO"
Case #EXCEPTION_ACCESS_VIOLATION
Fehler$ = "Error: ACCESS VIOLATION"
Case #EXCEPTION_ARRAY_BOUNDS_EXCEEDED
Fehler$ = "Error: ARRAY BOUNDS EXCEEDED"
Case #EXCEPTION_DATATYPE_MISALIGNMENT
Fehler$ = "Error: DATATYPE MISALIGNMENT"
Case #EXCEPTION_ILLEGAL_INSTRUCTION
Fehler$ = "Error: ILLEGALINSTRUCTION"
Case #EXCEPTION_STACK_OVERFLOW
Fehler$ = "Error: STACK OVERFLOW"
Default
Fehler$ = "Error: Unknown error"
EndSelect
; Pop up a message box
Titel$ = "Oops" +Chr(0)
Fehler$ + Chr(13) + "I will now exiting the program. See 'ya"
Procedure_Result.l = MessageBox_ (#NULL, @Fehler$, @Titel$, #MB_OK | #MB_ICONSTOP)
_Cleanup() : End
EndIf
EndProcedure
; ---------------------------------------------
; MAIN PROGRAM
; ---------------------------------------------
; Activate our Errorhandler
Result.l = SetUnhandledExceptionFilter_(@_MyExceptionFilter())
If OpenWindow(0,100,100, 400, 200, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget ,"Crashtest")
; Now let us rise a Division by Zero for example
IamZero.l = 0
IwillCrash.l = 1 / IamZero.l
; The following code will never be executed :wink:
Repeat
EventID.l = WaitWindowEvent()
Until EventID.l = #WM_CLOSE
EndIf
; Restore Errorhandler before exiting the program
Procedure_Result.l = SetUnhandledExceptionFilter_(0)
End
Registered PureBasic Coder
Edited by - FAKEFACTORY on 27 February 2002 04:54:39