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事件. 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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |