3

I have a threaded application and for some purpose I want to pass call stack information of a catched exception to a new custom exception:

try
    //here an unknown exception is rissen
except 
    on E: Exception do
    begin
        if ... then
          raise EMyException.Create(E, CallStackOfExceptionEAsString);
    end;
end;

What is the best way to do this, preferably using EurekaLog? I am using Delphi 2006 btw.

Alois Heimer
  • 1,772
  • 1
  • 18
  • 40
  • Does D2006 have the `StackTrace` and `StackInfo` properties of the `Exception` class? If so EurekaLog will have populated them with the info you need. – David Heffernan Jul 05 '13 at 09:35
  • No D2006 does not provide these properties. They are [available since D2009](http://stackoverflow.com/questions/286628/hooking-a-stacktrace-in-delphi-2009). – Alois Heimer Jul 05 '13 at 10:14
  • @DavidHeffernan: Why did you remove the tag eurekalog? The question and the answers refer to EurekaLog? – Alois Heimer Jul 08 '13 at 08:44
  • Because it's not a noteworthy enough subject for a tag. It's too narrow. There are only 8 questions in total with that tag. But there are hundreds of questions relating to EurekaLog. So it's misleading. You might click on the tag and think that you've got all questions relating to EurekaLog. – David Heffernan Jul 08 '13 at 08:48
  • @DavidHeffernan: I am not the same opinion, I think either the tag itself should be removed, or otherwise it should be applied, if appropriate. But I am new here ... :) – Alois Heimer Jul 08 '13 at 10:16
  • I think the tag should be removed. Your use of the exception-handling tag is also somewhat pointless in my opinion. What benefit will that bring? – David Heffernan Jul 08 '13 at 10:29
  • @DavidHeffernan: I think tags should be used to classify the questions asked. IMHO every question should have a tag which summarizes the theme it covers (not only the used technology). For this, I would prefer the eurekalog tag. But if this is to be deleted, than I think there should be another one. exception-handling was the best match I could find. The benefit: If I am an EurekaLog-expert i can still search for `[delphi] [exception-handling]`. – Alois Heimer Jul 08 '13 at 11:51
  • That would work if questions were reliably tagged for more off-beat tags. But they are not. They are generally well tagged for things like delphi, c, c#, windows, linux etc. For eurekalog, not at all. The way to search for eurekalog on SO is to use websearch. For example this search: `site:stackoverflow.com eurekalog delphi` – David Heffernan Jul 08 '13 at 11:52
  • @DavidHeffernan: A truely intelligent tag system would detect the `eurekalog`-tag, find that this is a sub tag to `exception-handling` and would add that, if it is missing :-). But either way, I can live with all proposed variants of tagging for this question, my problem is already solved. :-) Btw. your hint is a valuable answer for users of 2009 and better, so I would recommend to add this as an answer. – Alois Heimer Jul 08 '13 at 12:15
  • Tagging and search on SO is the biggest weakness of the site in my view. Tagging requires manual intervention. As was proved by Yahoo/Google 10 years ago, manually indexing a large body of somewhat free-form data cannot match crawler based indexing. – David Heffernan Jul 08 '13 at 12:18
  • A proposal regarding better tagging http://meta.stackexchange.com/a/45513/227669 – Alois Heimer Jul 08 '13 at 13:40

3 Answers3

2

EurekaLog exposes several event handlers like OnExceptionNotify.

You can implement these in your code. For example: procedure EurekaLogExceptionNotify( EurekaExceptionRecord: TEurekaExceptionRecord; var Handled: Boolean);

Here you can see a TEurekaExceptionRecord which is defined in ExceptionLog.pas. But you maybe just own the non-source version which works just fine.

The record has a EurekaExceptionRecord.CallStack list. This proprietary list can be converted to TStringsusing the CallStackToStrings method which is also defined in the ExceptionLog unit.

Here is an example where I write the CallStack into a StringList.

CallStackList := TStringList.Create;
try
  CallStackToStrings(EurekaExceptionRecord.CallStack, CallStackList);

  LogMessage := 'An unhandled exception occured. Here is the CallStack.' + #13#10
    + CallStackList.Text;
finally
  CallStackList.Free;
end;

At least from this starting point you should be able to investigate the exposed functions, records etc.. All information is accessible.

Erik Virtel
  • 810
  • 2
  • 9
  • 27
  • Thank you for showing a way to go. I hoped there would be a more simple solution, because handling of these callbacks wont be easy in my multithreaded app. – Alois Heimer Jul 05 '13 at 12:11
2

EurekaLog provides a function GetLastExceptionCallStack() (defined in unit ExceptionLog.pas). Using this I have written the following function (based on example code here):

function GetLastEurekalogCallStackAsString(): string;
{$IFDEF EUREKALOG}
var
  Stack: TEurekaStackList;
  Str: TStringList;
{$ENDIF}
begin
{$IFDEF EUREKALOG}
    Stack := GetLastExceptionCallStack();
    try
        Str := TStringList.Create;
        try
            CallStackToStrings(Stack, Str);
            Result := Str.Text;
        finally
            FreeAndNil(Str);
        end;
    finally
        FreeAndNil(Stack);
    end;
{$ELSE}
    Result := '';
{$ENDIF}
end;

So you can write:

try
    //here an unknown exception is rissen
except 
    on E: Exception do
    begin
        if ... then
          raise EMyException.Create(E, GetLastEurekalogCallStackAsString());
    end;
end;
yonojoy
  • 5,486
  • 1
  • 31
  • 60
0

EurekaLog 7 has Chained Exception support, which is specifically designed for this task. Just enable it in options (it is enabled by default) and use:

try
  // here an unknown exception is rissen
except 
  on E: Exception do
  begin
    if ... then
      Exception.RaiseOuterException(EMyException.Create(E.Message));
      // for old IDEs:
      // raise EMyException.Create(E.Message);
  end;
end;
Alex
  • 5,477
  • 2
  • 36
  • 56