Aktuelle Zeit: 24.08.2019 06:33

Alle Zeiten sind UTC + 1 Stunde [ Sommerzeit ]




Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 
Autor Nachricht
 Betreff des Beitrags: Try/Catch Errorhandler für Windows/Linux 32/64 Bit
BeitragVerfasst: 03.07.2009 19:43 
Offline
Benutzeravatar

Registriert: 10.12.2004 19:27
Hallo

Ich habe einen Try/Catch Errorhandler geschreiben, dieser Funktioniert unter Windows und Linux bei 32 und 64 Bit.


Code:
;****************************
;***  Try/Catch Commands  ***
;****************************
;*** (c) 2009 RocketRider ***
;***    RocketRider.eu    ***
;****************************
;***    PureBasic 4.31    ***
;****************************
;+ Supports Windows/Linux 32/64 Bit
;+ Does not support multi threated applications at this time

DisableDebugger
Global __TRYCATCH_pOnErrorAddress.i
Global __TRYCATCH_pOldOnErrorAddress.i
Global __TRYCATCH_pTryCatchStackPointer.i
Global __TRYCATCH_pOldTryCatchStackPointer.i

__TRYCATCH_pOnErrorAddress = ?__TRYCATCH_DefaultExceptionHandler
OnErrorGoto(?__TRYCATCH_DefaultExceptionHandler)
Goto __TRYCATCH_EndDefaultExceptionHandler
__TRYCATCH_DefaultExceptionHandler:
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  MessageRequester("Unhandled Exception!", ErrorMessage(), #MB_ICONERROR)
CompilerElse
  MessageRequester("Unhandled Exception!", ErrorMessage())
CompilerEndIf
End
__TRYCATCH_EndDefaultExceptionHandler:

Macro Try(Name)
  __TRYCATCH_pOldOnErrorAddress = __TRYCATCH_pOnErrorAddress
  __TRYCATCH_pOldTryCatchStackPointer = __TRYCATCH_pTryCatchStackPointer
  __TRYCATCH_pOnErrorAddress = ?__TRYCATCH_#Name
 
  CompilerIf #PB_Compiler_Processor =#PB_Processor_x86
    !MOV dword[v___TRYCATCH_pTryCatchStackPointer],ESP
  CompilerElse
    CompilerIf #PB_Compiler_Processor =#PB_Processor_x64
      !MOV qword[v___TRYCATCH_pTryCatchStackPointer],RSP
    CompilerElse
      CompilerError "Processor not supported by try/catch handler"
    CompilerEndIf
  CompilerEndIf
 
  OnErrorGoto(?__TRYCATCH_#Name)
EndMacro
Macro Catch(Name)
  Goto __TRYCATCH_End#Name
  __TRYCATCH_#Name:
  CompilerIf #PB_Compiler_Processor =#PB_Processor_x86
    !MOV ESP,dword[v___TRYCATCH_pTryCatchStackPointer]
  CompilerElse
    CompilerIf #PB_Compiler_Processor =#PB_Processor_x64
      !MOV RSP,qword[v___TRYCATCH_pTryCatchStackPointer]
    CompilerElse
      CompilerError "Processor not supported by try/catch handler"
    CompilerEndIf
  CompilerEndIf
  __TRYCATCH_pTryCatchStackPointer = __TRYCATCH_pOldTryCatchStackPointer
  __TRYCATCH_pOnErrorAddress = __TRYCATCH_pOldOnErrorAddress
  OnErrorGoto(__TRYCATCH_pOnErrorAddress)
EndMacro
Macro EndCatch(Name)
  __TRYCATCH_End#Name:
  __TRYCATCH_pTryCatchStackPointer = __TRYCATCH_pOldTryCatchStackPointer
  If __TRYCATCH_pOnErrorAddress <> __TRYCATCH_pOldOnErrorAddress
    __TRYCATCH_pOnErrorAddress = __TRYCATCH_pOldOnErrorAddress
    OnErrorGoto(__TRYCATCH_pOnErrorAddress)
  EndIf
EndMacro



;Test exception handling
Try(INTERRUPT_TEST)  ; Use Try to begin the exeption handling. INTERRUPT_TEST is the name of the try/catch section. It can be anything, but must be unique.
  ;Programcode which causes an exception
  !int 21h

Catch(INTERRUPT_TEST)  ; continues execution after Catch(...) if an exception occured, or jumps directly to EndCatch(...) if no exception occured.
  ;This part will be only called if an exception was raised.
  MessageRequester("Exception found!", ErrorMessage())
EndCatch(INTERRUPT_TEST) ; end try/catch section
;The stack will be corrected and the program can be continued.


; Now a second test...
Procedure Test()
  bResult = #True
  Try(POKEL_TEST)
    ;Cause an exception
    PokeL(0,0)
 
  Catch(POKEL_TEST)
    ;This part will be only called if an exception was raised.
    MessageRequester("Exception found in Procedure Test()!", ErrorMessage())
    ;Do not use ProcedureReturn into a Catch...EndCatch section, because exeption handle would not work correct, if nested try-blocks are used (EndCatch would be not called in this case).
    ;Instead st return value
    bResult = #False
  EndCatch(POKEL_TEST)
  ProcedureReturn bResult
EndProcedure

MessageRequester("Result", Str(Test()))




Gruß
RocketRider

_________________
GreenForce-Player - Der alternative Media Player!
Wie viele Tage sind es von Halloween bis Weihnachten?
Okt 31 - Dez 25 = 0 Tage!


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 1 Beitrag ] 

Alle Zeiten sind UTC + 1 Stunde [ Sommerzeit ]


Wer ist online?

Mitglieder in diesem Forum: Google [Bot] und 1 Gast


Sie dürfen keine neuen Themen in diesem Forum erstellen.
Sie dürfen keine Antworten zu Themen in diesem Forum erstellen.
Sie dürfen Ihre Beiträge in diesem Forum nicht ändern.
Sie dürfen Ihre Beiträge in diesem Forum nicht löschen.

Suche nach:
Gehe zu:  

 


Powered by phpBB © 2008 phpBB Group | Deutsche Übersetzung durch phpBB.de
subSilver+ theme by Canver Software, sponsor Sanal Modifiye