views:

324

answers:

4

In one of our application im getting an exception that i can not seem to find or trap.

...
  Application.CreateForm(TFrmMain, FrmMain);
  outputdebugstring(pansichar('Application Run')); //this is printed
  Application.Run;
  outputdebugstring(pansichar('Application Run After')); //this is printed
end.
<--- The Exception seems to be here

The Event log shows

> ODS: Application Run 
> //Various Application Messages 
> ODS: Application Run After
> First Change Exception at $xxxxxxxx.  ...etc

All i can think of is it is the finalization code of one of the units.

(Delphi 7)

+4  A: 

Try installing MadExcept - it should catch the exception and give you a stack-trace.

It helped me when I had a similar issue.

Blorgbeard
+4  A: 

Here's two things you can try:

1) Quick and easy is to to hit 'F7' on the final 'end.'. This will step you into the other finalization blocks.

2) Try overriding the Application.OnException Event.

Zartog
+3  A: 

The SysUtils unit actually sets up the default ErrorProc and ExceptProc procedures in its initialization section, and undoes them in its finalization section, so often in this situation it's worth ensuring that SysUtils is the very first unit in the uses clause in your dpr, so then it will be the last one finalised. Might be enough to get you some meaningful data about what is going wrong.

Malcolm Groves
+1  A: 

Finalization exceptions are tricky. Even if you put SysUtls first in your project file, your application object may already be gone, which means your global exception handler is gone too. MadExcept may work for this though.

Another solution is to put a Try / Except block in each of your unit finalization sections, and then handle the exceptions there.

What is your objective? Do you want to suppress the exception or debug it? Debugging it can be done by stepping through them with F7 as Zartog suggested. If you discover which unit has the exception in finalization then you might try placing it in a different order in the uses clause it is called from.

Good luck!

Jim McKeeth