Hei*_*cht 11 delphi exception-handling
我的代码很好地涵盖了异常处理(try..except).预计不会发生一些异常,并且会经常发生一些异常,这是预期的,也可以.现在我想为这段代码添加一些自动化测试.知道在执行期间发生了多少异常会很好,所以我稍后可以看到是否提出了预期的数字或发生了什么意外.我不想用调试代码混淆每个异常处理块,所以我的问题是:
有没有办法安装某种全局异常处理程序,它位于所有其他异常处理块之前?我正在寻找一个记录这些例外的中心位置.
谢谢你的任何建议!
(如果这很重要:它是Delphi 2009)
da-*_*oft 16
您可以执行以下操作:
有关详细信息,请参阅上面的变量和过程声明.
我认为您可以使用AddVectoredExceptionHandler API函数.
这是一个关于如何使用的小样本:
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);
Run Code Online (Sandbox Code Playgroud)