加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何记录源代码行导致异常并添加自定义信息?

发布时间:2020-12-15 09:39:55 所属栏目:大数据 来源:网络整理
导读:我们的应用程序使用JCL记录导致异常的源代码行,并且它运行良好. 我用的是D2007.我有一个执行实际日志记录的TApplicationEvents.OnException事件. 考虑一下: function MyFunc: String;begin // Codelines that may raise exception. // Call functions that
我们的应用程序使用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;

我有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,20);
end;

现在记录了myvar的值,但是我放弃了异常的原始源代码.而是记录带有raise TException.CreateFmt的行.有关如何做到这两点的任何建议?

问候

解决方法

除了Marjan Vennema的答案(我会遵循),你可以提出新的异常并使它看起来像是来自旧异常的地址.

except
  on E: Exception do
    raise Exception.CreateFmt('myvar = %s',[myvar]) at ExceptAddr;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读