-1

I'm using Delphi 11. Considering this example code :

try
raise exception.create('Test');
except
on e : exception do
begin
showmessage(e.message+' on form X, line Y');
end;
end;

Is there a way to get the exact form X and line Y where the exception occured ?

delphirules
  • 6,443
  • 17
  • 59
  • 108
  • Omitting indention is just half of the right way - now put all statements into one line (that's possible thanks to the `;`), then it will always be line #1. – AmigoJack Sep 13 '22 at 12:47
  • @AmigoJack: But that doesn't give you the form :-) – HeartWare Sep 13 '22 at 12:49
  • @HeartWare There must be a reason why OP did not issue `self.Name`, but he didn't mention it. If that code is not a in a method of the form's class then it also doesn't happen in a "_form_" - only in a _unit_. – AmigoJack Sep 13 '22 at 12:54
  • @AmigoJack: There's no guarantee that the code is located within a TForm's methods. It might as well be in a library function with no access to the (correct) "Self" variable. Or it might be within a CLASS FUNCTION of a completely different TForm descendant than the "active" TForm instance. – HeartWare Sep 13 '22 at 12:56
  • @HeartWare I know, and all those cases are then in a _unit_, too. That's the point where one learns to fully understand the terms and their meaning. To realize what to better put into a class. Or that the substituted content of `{$I file.inc}` cannot have line numbers. – AmigoJack Sep 13 '22 at 13:06
  • @AmigoJack I use indention on Delphi, but this code was typed directly on SO, and it doesn't have the CTRL+D option :-p – delphirules Sep 13 '22 at 13:21
  • If you have debugging enabled, the IDE will tell put the cursor there. – Rohit Gupta Sep 13 '22 at 13:27
  • @RohitGupta Of course but i mean on production environment – delphirules Sep 13 '22 at 13:28
  • As mentioned below there is EurekaLog, but I prefer [**madExcept**](http://www.madexcept.com/madExceptDescription.htm). But its not free for commercial use – Rohit Gupta Sep 13 '22 at 13:31
  • 1
    Perhaps you find useful: `Assert(False, e.message);` gives `Test (D:\Delphi\AProjects\Cons\Project3.dpr, line 15)` – MBo Sep 13 '22 at 15:58

2 Answers2

2

I would recommend madExcept: it's free for personal use but not for commercial use. It is very configurable and allows the user to send a bug report.

dialog on unhandled exception

From madshi > Help > madExcept:

The usual customer doesn't really want to read madExcept's detailed bug report. He just wants to send the bug report to the programmer, hoping that this will lead to a corrected new version. So in the default settings madExcept's box doesn't show any exception information, not even the exception message itself. The customer can just mail the bug report, or he can continue, restart or close the application.

One thing should be mentioned here: madExcept's exception box is not using any VCL stuff, instead it's build on pure win32 API calls. It's no fun designing a nice box with bitmap buttons and so on with win32 APIs only, but in this case it was absolutely necessary. The VCL is not thread safe, but our exception box must be able to show up in the context of each thread - thus it has to be thread safe, thus it must not use the VCL.

Apart from pure exception handling madExcept does one more thing (if you want it to), namely checking whether the main thread is frozen or not. Sometimes your program doesn't react anymore, although no exception occured. That can happen e.g. if your main thread runs in an infinite loop or if there's a bug in the synchronization of multiple threads, with the result of a dead lock. Such a bug can be very difficult to find, because your program is just frozen, without showing an exception. But with madExcept the situation is not that hopeless anymore: If your main thread doesn't react on messages for a specified time, madExcept raises an exception in the context of the main thread. In the resulting bug report you can see what the main thread was doing exactly and also what all other threads were doing at that time. That should help a lot finding infinite loop and dead lock bugs.

I am not associated with madshi.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
  • Yes, it's great but i have a problem with Asprotect : https://stackoverflow.com/questions/73704528/delphi-11-with-madexcept-and-asprotect – delphirules Sep 13 '22 at 14:14
1

No, there isn't. Not using plain Delphi. Delphi doesn't have the __FILE__ or __LINE__ macros that C/C++ have.

You can get such info in your reports using an add-on exception handler, like EurekaLog.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
HeartWare
  • 7,464
  • 2
  • 26
  • 30