April 17, 2005
Hello,

Is there a method for handling exceptions thrown by module static constructors?


Regards,
Darren Grant
April 17, 2005
This is an example from HelloWorld.d win32 it popups "Initalization Error"
MB
on exceptions thrown in

      _moduleCtor();      // call module constructors


extern (Windows)
int WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    int result = 0;

    gc_init();          // initialize garbage collector
    _minit();           // initialize module constructor table

    try
    {
      _moduleCtor();      // call module constructors
      _moduleUnitTests(); // run unit tests (optional)
    }
    catch (Object o)        // catch any uncaught exceptions
    {
      MessageBoxA(null, cast(char *)o.toString(), "Initalization Error",
            MB_OK | MB_ICONEXCLAMATION);
      result = -2;     // failed
    }


"Darren Grant" <Darren_member@pathlink.com> wrote in message news:d3t9tq$1csq$1@digitaldaemon.com...
> Hello,
>
> Is there a method for handling exceptions thrown by module static constructors?
>
>
> Regards,
> Darren Grant


April 18, 2005
Darren Grant wrote:
> Hello,
> 
> Is there a method for handling exceptions thrown by module static constructors?

When using WinMain as an entry point, yes, because you are required to initialize everything yourself, including module constructors. When using main as an entry point, no, unless you go in and modify the extern(C) main method in phobos/internal/dmain2.d and recompile phobos.
April 18, 2005
In article <d3vii7$8rv$1@digitaldaemon.com>, Mike Parker says...
>
>Darren Grant wrote:
>> Hello,
>> 
>> Is there a method for handling exceptions thrown by module static constructors?
>
>When using WinMain as an entry point, yes, because you are required to initialize everything yourself, including module constructors. When using main as an entry point, no, unless you go in and modify the extern(C) main method in phobos/internal/dmain2.d and recompile phobos.

OK, thanks for the information.  What are other people doing given this limitation?

I am wondering how many cases there are where you'd want to catch module ctor exceptions at all.


Regards,
Darren Grant
April 18, 2005
> I am wondering how many cases there are where you'd want to catch module
> ctor
> exceptions at all.

The best design thing you can do is
to avoid exceptions thrown out of constructors.
This is good in many senses and works for me
many years.


Top | Discussion index | About this forum | D home