www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - handling module ctor exceptions

reply Darren Grant <Darren_member pathlink.com> writes:
Hello,

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


Regards,
Darren Grant
Apr 17 2005
next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
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 

Apr 17 2005
prev sibling parent reply Mike Parker <aldacron71 yahoo.com> writes:
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.
Apr 17 2005
parent reply Darren Grant <Darren_member pathlink.com> writes:
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
Apr 18 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 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.
Apr 18 2005