Errorhandler (functional)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by FAKEFACTORY.

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TronDoc.

woah! HEAVY stuff..kool
Maybe I can figure
out how to use it too!

I'm sure I'll find a
use for it down the road....jb

elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE :wink:
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by DarkUni.

Thanks for posting the code to the PB Basement!

Shane R. Monroe, Dark Unicorn Productions
Pure Basic Basement
http://pb.darkunicornproductions.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi FakeFactory
Nice example... i saw it some time ago somwhere in the net as c++ example :wink: I have tested your example but seems there is a small problem...!?

Code: Select all

[u]you wrote:[/u]
   ; The following code will never be executed :wink:
   ;
   Repeat
      EventID.l = WaitWindowEvent() 
   Until EventID.l = #WM_CLOSE
Mhhh... i get your requester but seems that the following lines will be executed on my system too...!?

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by FAKEFACTORY.

Huh?

Your program continues *after* a division by zero???
Wow, we should swap the computers.

;)

don't know, how this can happen. Have you any anti-crash soft like CrashProf installed.

Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.
FakeFactory wrote:
Your program continues *after* a division by zero???
Wow, we should swap the computers. :wink:
Hi FakeFactory
hehe... yes, the program will still continue executing all next operations..
don't know, how this can happen. Have you any anti-crash soft like CrashProf installed.
Sorry, i dont know too... i dont have any patched/hacked system related stuff... just a normal Win98 system... strange!


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Your program continues *after* a division by zero???

I doubt it. Obviously the error handler catches it BEFORE it occurs, and
then reports the error via the API.


PB - Registered PureBasic Coder
Post Reply