Is there any way to catch unhandled application exceptions and log them

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)?


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.

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:

"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 ( 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.

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.

use try..except block like this:

// a critical section here
except on E:exception do
  showmessage('an error occured: ' + E.message);
  //do something else

Need Your Help

Where do PEAR packages normally get installed?

php pear

I am using a Debian squeeze server. I have installed PEAR using aptitude install php-pear. This created a directory /usr/share/php/PEAR.