Rohit,

If I were you I would not put anything that can cause an exception in the
initialization / finalization part of a unit.  The reasons are:

1.  SysUtils may not yet be initialized, or have been finalized.  As this
unit is responsible for exception handling, your users will get Error 216
instead of a meaningful error message.
2.  As you have found out, trapping exceptions is problematic.
3.  If the unit is used in a DLL / package, the finalization code is not
always called, even when the DLL is finally unloaded.
4.  There is no direct way to control the unit initialization order.  I have
a project that initializes its units differently depending on whether I do a
full rebuild or an incremental compile.
5.  It is easier to debug through initialization / finalization code if they
are explicitly called.

The only things that I put in initialization units are:

1.  Code to set unit level pointers / object references to Nil
2.  Code to free unit level objects (if this fails, at least the
consequences are not that great) - but not in a DLL / package.

I, for one wouldn't create forms and data modules in initialization.  What I
have tended to prefer is to initialise the object reference to Nil.  Then
when the object is refereed to, create the object if Nil.

In you app, you might have to resort to letting the Application object
handle the exception, then call Abort.


Dennis.

>
> Dennis,
>
> It was a bad choice of words.  I lumped formcreate code into
> initialisation.  Any non-trivial code to deal with maths, strings,
> objects, forms etc can cause exceptions.
>
> One of the things that happens in our apps is that they use a
> commandline parameter to determine where the data files are - so
> that one exe can work with multiple data sets.  This is set in the
> datamodule for the project which contains the TDatabase component
> for the project.  Before I put the try except in, an invalid path
> would cause each form to generate multiple exceptions.  So I
> have trapped it, and that works.
>
> The problem remaining is that the VCL still gives me two (sometimes
> 3) more exceptions after that.  I dont really want to use a
> messagebox to display the exception message and use halt.  But I
> might have to.
>
> Your point about the initialisation code is taken.  I hadnt yet
> thought about it, but it is a worry as well.  There should be
> automatic try-except block around the main dpr code.


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to