Our application registers the source string, throwing an exception with the JCL, and works just fine. I am using D2007. I have a TApplicationEvents.OnException event that does the actual logging. Consider this:
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;
I have 3 methods and my onException event. LogLastException logs the column when an exception occurs. The problem is that I cannot add information to E.Message without losing the source that throws the exception. Pretend this is the second line in ComplexFunc that throws an exception. I also want to write the value of the myvar variable. Therefore, I change the code to:
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;
Now the myvar value is registered, BUT for the price I lose the original exception surrolin. Instead, a line with a raise of TException.CreateFmt is written. Any suggestion on how to do both?
Hello
source share