Search code examples
delphidelphi-xe

How to display line number where an exception happens during runtime without a debugger?


I have an exception happening during runtime which is non reproducible and therefore I can't debug it in the IDE/debugger. I want to know where in the code this happens so I surrounded the code with try/except statement, inserted a 'raise exception' statement for testing & displayed the stacktrace like this to see if it works:

   on e: exception do begin
          showmessage(e.StackTrace);
    end;

However the message displayed was empty. Why was it empty? Is there another way to know where an exception happened? Using Delphi XE.


Solution

  • The Exception.StackTrace documentation indicates that the default implementation returns an empty string, and that in order to use the functionality you need to provide a GetStackInfoStringProc procedure. It also suggests some implementations (both free and commercial) from third-party providers that can be used instead of writing your own.

    Here's a (brief) excerpt from that documentation - visit the documentation page for the related links:

    By default, StackTrace is always an empty string. If you want StackTrace to contain an actual value, you must assign GetStackInfoStringProc a procedure that can generate a string from the stack information.

    Using Exception.StackTrace

    In order to use the StackTrace property to obtain the stack trace for exceptions, you have to use (or implement) a stack trace provider. There are a number of third-party solutions, both commercial and free.

    Some of the stack trace providers are:

    For more information on how to use some of the above third-party stack trace providers, see the following blog posts: