Announcement

Collapse
No announcement yet.

Exceptions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exceptions

    Today I played a little with exceptions.
    I looked Florent's code, MSJ C/ASM code and some moments confused me.
    1) strange values:
    %ExceptionContinueExecution = 0 ...
    I know that this is EXCEPTION_DISPOSITION's values valid for C except.
    But why ExceptionContinueExecution <> Exception_Continue_Execution ?
    Because of C implementation or something else ?
    2) is it really necessary to save ESP / EBP in similar situations ?
    And what happends, if do not save ?

    My own test also confuses me.
    Ok, first FNCLEX is possible to explain.
    But why I can't avoid second FNCLEX in OnError ? (I am under Win2000).

    Code:
      #Compile Exe
      #Dim All
      #Register None
      #Include "WIN32API.INC"
    
      '==============================================================================================
      Type EXCEPTION1_POINTERS ' <--- Because of mistake in Win32Api.Inc
         pExceptionRecord As EXCEPTION_RECORD Ptr
         pContextRecord As CONTEXT Ptr
      End Type
    
      Global GPFCode As Dword, GPFContAddr As Dword
    
      Function GPFMsg(Code As Dword) As String
         Select Case Code
            Case %EXCEPTION_ACCESS_VIOLATION      : Function = "Access violation"
            Case %EXCEPTION_DATATYPE_MISALIGNMENT : Function = "Datatype misalgnment"
            Case %EXCEPTION_BREAKPOINT            : Function = "Breakpoint"
            Case %EXCEPTION_SINGLE_STEP           : Function = "Single step"
            Case %EXCEPTION_ARRAY_BOUNDS_EXCEEDED : Function = "Array bounds exceeded"
            Case %EXCEPTION_FLT_DENORMAL_OPERAND  : Function = "Float denormal operand"
            Case %EXCEPTION_FLT_DIVIDE_BY_ZERO    : Function = "Float divide by zero"
            Case %EXCEPTION_FLT_INEXACT_RESULT    : Function = "Float inexact result"
            Case %EXCEPTION_FLT_INVALID_OPERATION : Function = "Float invalid operation"
            Case %EXCEPTION_FLT_OVERFLOW          : Function = "Float overflow"
            Case %EXCEPTION_FLT_STACK_CHECK       : Function = "Float stack check"
            Case %EXCEPTION_FLT_UNDERFLOW         : Function = "Float underflow"
            Case %EXCEPTION_INT_DIVIDE_BY_ZERO    : Function = "Integer divide by zero"
            Case %EXCEPTION_INT_OVERFLOW          : Function  = "Integer Overflow"
            Case %EXCEPTION_PRIV_INSTRUCTION      : Function = "Privileged instruction"
            Case %EXCEPTION_IN_PAGE_ERROR         : Function = "In page error"
            Case Else                             : Function  = "Exception &H" + Hex$(Code, 8)
         End Select
      End Function
    
      Function OurUnhandledExceptionFilter(Exception_Pointers As EXCEPTION1_POINTERS) As Long
         ! FNCLEX
         GPFCode = [email protected]
         Function =  %EXCEPTION_CONTINUE_SEARCH
         If IsFalse(VarPtr(Exception_Pointers)) Then
         ElseIf IsFalse(Exception_Pointers.pExceptionRecord) Then
         ElseIf [email protected] = %EXCEPTION_NONCONTINUABLE Then
         ElseIf IsFalse(GPFContAddr) Then
            MsgBox "You crashed the PC." + $CRLF + "Time to buy new one.", %MB_ICONEXCLAMATION, "Dr. Watson"
            Function = %EXCEPTION_EXECUTE_HANDLER
         Else
            [email protected] = GPFContAddr
            Function = %EXCEPTION_CONTINUE_EXECUTION
         End If
      End Function
      '=============================================================================
      
      Function PbMain() As Long
         Dim x As Integer, y As Integer, Stmt As Long
         SetUnhandledExceptionFilter ByVal CodePtr(OurUnhandledExceptionFilter)
         Dim s As Long
         ! FSTCW s
         s = s And &HFF30   ' Allow FPU exceptions
         ! FLDCW s
    
         GPFContAddr = CodePtr(OnError)
         
         
         Stmt = 1: x = 0: x = x / y
    
      Step2:
         Stmt = 2: x = 10: x = x / y
    
      Step3:
         GPFContAddr = 0
    
         ' Test for access violation
         Dim b As Byte Ptr
         @b = 0
        
         MsgBox "Finished" ' <-- Doesn't work, because we already exited
         Exit Function
    
      OnError:
         ! FNCLEX
         MsgBox GPFMsg(GPFCode) + " at statement" + Str$(Stmt), %MB_TASKMODAL, "Program Error"
         If Stmt = 1 Then GoTo Step2
         If Stmt = 2 Then GoTo Step3
      End Function
    ------------------
    E-MAIL: [email protected]

  • #2
    Not sure if I can help at all but when I want to *cause* one I just do this:


    RaiseException %EXCEPTION_ACCESS_VIOLATION, 0, 0, 0

    ------------------
    Scott
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      Code:
        Type EXCEPTION1_POINTERS ' <--- Because of mistake in Win32Api.Inc
      I gather that any mistake there has since been fixed. If not, please let us know.

      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment

      Working...
      X