Rol*_*son 4 delphi exception-handling delphi-2007
我们的应用程序使用JCL记录导致异常的源代码行,并且它运行良好.我用的是D2007.我有一个执行实际日志记录的TApplicationEvents.OnException事件.考虑一下:
function MyFunc: String;
begin
// Codelines that may raise exception.
// Call functions that also may raise exception
end;
procedure ComplexFunc(aVariable: String);
begin
// also here can it be exceptions....
// Code here that is the cause of exception
end;
procedure foo;
var
myVar: String;
begin
myvar := MyFunc;
ComplexFunc(myvar);
end;
procedure TMainForm.ApplicationEvents1Exception(Sender: TObject; E: Exception);
begin
LogLastException(E, 'Unhandled Exception (%s)', [E.Message], 20);
end;
Run Code Online (Sandbox Code Playgroud)
我有3个方法和我的onException事件.LogLastException在发生异常时记录callstack.问题是我无法在没有松散导致异常的源代码的情况下向E.Message添加信息.假设它是ComplexFunc中引发异常的第二行.我还想记录myvar变量的值.所以我将代码更改为:
function MyFunc: String;
begin
// Codelines that may raise exception.
// Call functions that also may raise exception
end;
procedure ComplexFunc(aVariable: String);
begin
// also here can it be exceptions....
// Code here that is the cause of exception
end;
procedure foo;
var
myVar: String;
begin
try
myvar := MyFunc;
ComplexFunc(myvar);
except
on E: Exception do
raise TException.CreateFmt('myvar = %s', [myvar]);
end;
end;
procedure TMainForm.ApplicationEvents1Exception(Sender: TObject; E: Exception);
begin
LogLastException(E, 'Unhandled Exception (%s)', [E.Message], 20);
end;
Run Code Online (Sandbox Code Playgroud)
现在记录了myvar的值,但是我放弃了异常的原始源代码.而是记录带有raise TException.CreateFmt的行.有关如何做到这两点的任何建议?
问候
除了Marjan Vennema的回答(我将遵循),你可以提出你的新异常并使它看起来像来自旧异常的地址.
except
on E: Exception do
raise Exception.CreateFmt('myvar = %s', [myvar]) at ExceptAddr;
end;
Run Code Online (Sandbox Code Playgroud)