Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 309
Tony_Henrich Avatar asked Dec 07 '22 22:12

Tony_Henrich


1 Answers

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:

  • JEDI Code Library (the JclDebug and the JCLHookExcept unit).
  • EurekaLog
  • madExcept

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

  • Working with Delphi’s new Exception.StackTrace (Tobias Gurock, 2009).
  • CodeVerge - How to use Exception.StackTrace (2008).
like image 141
Ken White Avatar answered Dec 28 '22 07:12

Ken White