Is it possible to have a global exception hook?

前端 未结 4 1711
南方客
南方客 2020-12-28 20:57

my code is fairly well covered with exception handling (try..except). Some exceptions are not expected to happen and some exceptions happen fairly often, which is expected a

相关标签:
4条回答
  • 2020-12-28 21:05

    I think you can use the AddVectoredExceptionHandler API function.

    Here is a small sample on how to use:

    var
      f : TFileStream;
    
        function VectoredHandler(ExceptionInfo : PEXCEPTION_POINTERS): LongInt; stdcall;
        var 
          s : String;
        begin
          S := Format('Exception code %x address %p'#10#13, [ExceptionInfo^.ExceptionRecord^.ExceptionCode,
           ExceptionInfo^.ExceptionRecord^.ExceptionAddress]);
           f.WriteBuffer(PChar(s)^, Length(s) * sizeof(wchar));
           FlushFileBuffers(f.Handle);
          OutputDebugString(PChar(Format('ExceptionCode: %x', [ExceptionInfo^.ExceptionRecord^.ExceptionCode])));
          result := EXCEPTION_CONTINUE_SEARCH ;
        end;
    
    
        initialization
          AddVectoredExceptionHandler(0, VectoredHandler);
    
    0 讨论(0)
  • 2020-12-28 21:10

    JCL has it's own exception dialog. Just add this dialog to your project, it will handle all unexpected exceptions. Detailed info located in this JCL folder: jcl\experts\debug. There is also howto text file which step by step describes how to use it.

    0 讨论(0)
  • 2020-12-28 21:20

    You can do the following:

    • preserve the value of System.RaiseExceptObjProc variable, which in normal Delphi app is pointing to SysUtils.RaiseExceptObject
    • create your own "RaiseExceptObject" proc and assign it to the RaiseExceptObjProc variable
    • in your own "RaiseExceptObject" proc you can do what you want, then call saved RaiseExceptObjProc value

    For details, see above variable and procedure declarations.

    0 讨论(0)
  • 2020-12-28 21:27

    You could add a custom handler to madExcept which would then allow you to get a full stack trace, but also carry on.

    0 讨论(0)
提交回复
热议问题