is there any way to catch and log all errors in application. In the moment I use try catch blocks in places where I think error can occur. But is there a possibility to catch all errors in application level (I mean, can I put try catch block to project file or maybe some other trick will do it)?
-
You'd need to be able to define every conceivable type of error that would occur in your application and at what level, then surround them in try-catch blocks and/or log them somehow, like to the event log (assuming Windows) or a log file perhaps. – Darth Continent Feb 17 '11 at 18:50
-
1You can use the `TApplicationEvents` component and assign something to the `OnException` event. This procedure will then recieve all *unhandled* exceptions. – Andreas Rejbrand Feb 17 '11 at 18:54
-
Why catch them? Usually you want to let them float up to the top level handler. – David Heffernan Feb 17 '11 at 18:58
-
2@David: It depends on the exception. If it's a result of a bug in your code, then yes; let it float up and be caught by your exception logger. If it's a result of something outside your control, though, ("network connection not available," for example,) you want to find a way to handle it that doesn't throw an error dialog in the user's face and make it look like there's a bug in your code when there isn't. – Mason Wheeler Feb 17 '11 at 19:20
-
Your question is unclear. You want to log all unhandled exceptions AND all handled exceptions? You want to catch, and squelch (ignore) all exceptions? What exactly? – Warren P Feb 18 '11 at 18:49
5 Answers
Take a look at MadExcept. If you add it into your project, it automatically installs hooks that will catch all unhandled exceptions, generate very informative error reports, and even email them to you or post them to a web service. It's the next best thing to actually being able to attach the debugger to your clients' systems.

- 82,511
- 50
- 270
- 477
You could also consider Eureka Log, which CodeGear themselves used for their PHP product. Eureka Log also has memory leak detection and allows you to have crash reports silently emailed, ftp'd, added to a bug tracking system automatically, or allows interactive crash dump submission.
It isn't free, but I think it is well worth the money. I honestly couldn't imagine using Delphi without it. Also, they have a .NET edition of the program as well.
One last feature that I love, is that you can actually configure the product to take different actions with certain exceptions, while still capturing others normally. I use this with some of the Indy exceptions that can be thrown:

- 13,248
- 9
- 69
- 119
-
+1 for this. We use EurekaLog at work. Its premium features are very useful, but a lot of the time MadExcept is "good enough," and it's free. – Mason Wheeler Feb 18 '11 at 15:48
-
1@Mason: MadExcept is only free for non-commercial applications. See: http://www.madexcept.com/madExceptShop.htm – lkessler Feb 18 '11 at 18:02
-
@lkessler: Good catch. I didn't even think of that distinction, because what I use it with *is* non-commercial. – Mason Wheeler Feb 18 '11 at 18:03
If you want to intercept ALL exceptions and log them, you need to implement a RTLUnwindProc
low-level procedure.
This is a bit low-level (e.g. it needs asm skills), so you should better rely on existing code. See this stack overflow question.
I even put some reference code (including low-level asm, working with Delphi 7 and later under Win32) in my own answer, which shows the logging features included in our Open Source mORMot framework. To log all exceptions, just add a reference to SynCommons.pas
and add the following line at program startup:
TSynLog.Family.Level := [sllException,sllExceptionOS];
Currently working from Delphi 5 up to XE4, for both Win32 and Win64 platforms.

- 1
- 1

- 42,305
- 3
- 71
- 159
"Catching" an exception can mean many things: logging it, displaying it, acting on it, reraising it, or any combination of the above. The TApplication OnException handler will "catch" all unhandled main thread exceptions, but will not catch exceptions raised in threads. To do this you will need to implement your own thread exception handling. In my application framework (http://www.csinnovations.com/framework_overview.htm) I have code that will ensure that any exception can be logged (all unhandled exceptions and optionally any handled exceptions), and optionally displayed if it is in the main thread, regardless of whether it occurs in the main thread or any other thread. While it is not as comprehensive as MadExcept, it has all the functionality that is needed to handle exceptions.

- 1,816
- 1
- 13
- 16
use try..except block like this:
try
..
// a critical section here
..
except on E:exception do
begin
showmessage('an error occured: ' + E.message);
//do something else
end
end;

- 325
- 2
- 7
-
-
But one vote for a code sample!!! Nice to know for newones coming to search for exception handling... – evilone Feb 18 '11 at 19:52