Page 1 of 1

Example of an Error Handler by FAKEFACTORY

Posted: Thu Jan 17, 2002 5:07 pm
by BackupUser
Code updated For 5.20+

Restored from previous forum. Originally posted by Franco.

Code: Select all

; Found this great peace of code on the german PureBasic forum.
; Coded by FakeFactory, translated from german to english by Franco.
; Well documented example of the implementation of an Error-Trap-Routine 
;
; You can add some code to receive information about what caused the error. (if you need...)
; For this look at MSDN or in a good C++ for Windows book.
 
Structure _EXCEPTION_POINTERS 
  *ExceptionRecord.EXCEPTION_RECORD; 
  *ContextRecord.CONTEXT; 
EndStructure 
 
Procedure _MyExceptionFilter(*lpEP._EXCEPTION_POINTERS) 
  ; This is your own Error Message Handler.
  ; If your program is causing an illegal operation you get a crash and a Windows message like:
  ; "This program has performed an illegal operation and will be shut down"
  ; But with this Procedure not anymore!
  ; Now you get your own MessageBox and the Program will be ended smoothly.
  ; The user will be delighted and the bug in YOUR code seems to be not so bad at all.
  
  Shared TerminateInProcess.b 

  If TerminateInProcess.b = #TRUE 

    ; Well if you reach this point there was an error in our own Error Message Handler.
    ; We are not able to end our program smoothly.
    ; In this case we can do nothing and the Operating System has to do the work...
    ; But first we have to close our own Error Message Handler.
  
    Result.l = SetUnhandledExceptionFilter_(0) 

    ; And now we get a nice crash controlled by the Operating System

    RaiseException_ (0,0,0,0) 

    End 
  Else 
    TerminateInProcess.b = #TRUE 
    ; OK we have now full control over the nasty bug and
    ; send a nice death announcement of the program...

    hWnd.l = WindowID(0) 
    Result.l = MessageBox_ (hWnd.l, "We truly apologize for this inconvenience.", "Ups... there was a problem.", #MB_OK | #MB_ICONSTOP) 
    End 
  EndIf 
EndProcedure 
 
; this point is the normal start of your code and we have to activate the Error Handler
Result.l = SetUnhandledExceptionFilter_(@_MyExceptionFilter()) 
 
; start of your code
; yada, yada, yada...
 
; this point is the normal end of your code and we have to deactivate the Error Handler
Result.l = SetUnhandledExceptionFilter_(0) 
Have a nice day...
Franco

BTW I didn't change the code above. But it's working without declaring the structure and without *lpEP._EXCEPTION_POINTERS when you call the procedure, it's not used inside the procedure anyway...


Edited by - franco on 17 January 2002 17:54:15

Posted: Thu Jan 17, 2002 6:27 pm
by BackupUser
Restored from previous forum. Originally posted by FAKEFACTORY.

You need the structure, if you want to parse the error and give some details on the error.



Registered PureBasic Coder