singleton not bubbling-up constructor exception

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

I am finding that when using a singleton to instantiate a class and a custom
exception is thrown in the constructor of the class being instantiated , in
the code that calls the singleton the original exception is not thrown.
Rather , a generic type initialization exception is thrown for the
singleton.

Does anyone know why this is ?



So,

public class MyClassInitException : Exception
{
public MyClassInitException( string message ) : base( message ) { }
}


public class MyClass
{
public MyClass()
{
// something goes wrong ...
// throw MyClassInitException
}
}


public class MyClassSingleton
{
private static MyClass _instance = new MyClass();

public MyClass Instance()
{
return _instance;
}
}



//calling code :

MyClass myClass = MyClassSingleton.Instance();

// exception is thrown "The type initializer for MyClassSingleton threw an
exception."
 
I am finding that when using a singleton to instantiate a class and a custom
exception is thrown in the constructor of the class being instantiated , in
the code that calls the singleton the original exception is not thrown.
Rather , a generic type initialization exception is thrown for the
singleton.

Does anyone know why this is ?

So,

public class MyClassInitException : Exception
{
    public MyClassInitException( string message ) : base( message ) {}

}

public class MyClass
{
    public MyClass()
    {
        // something goes wrong ...
        // throw MyClassInitException
    }

}

public class MyClassSingleton
{
    private static MyClass _instance = new MyClass();

    public MyClass Instance()
    {
        return _instance;
    }

}

//calling code :

MyClass myClass = MyClassSingleton.Instance();

// exception is thrown "The type initializer for MyClassSingleton threw an
exception."

Try moving the special Exception declaration (this line: public
MyClassInitException( string message ) : base( message ) { } ) into
the constructor. Since the class is not instantiated yet when the
error occurs, the fields of the class are not yet ready to use.
 
I am finding that when using a singleton to instantiate a class and a custom
exception is thrown in the constructor of the class being instantiated , in
the code that calls the singleton the original exception is not thrown.
Rather , a generic type initialization exception is thrown for the
singleton.

Does anyone know why this is ?

So,

public class MyClassInitException : Exception
{
    public MyClassInitException( string message ) : base( message ) {}

}

public class MyClass
{
    public MyClass()
    {
        // something goes wrong ...
        // throw MyClassInitException
    }

}

public class MyClassSingleton
{
    private static MyClass _instance = new MyClass();

    public MyClass Instance()
    {
        return _instance;
    }

}

//calling code :

MyClass myClass = MyClassSingleton.Instance();

// exception is thrown "The type initializer for MyClassSingleton threw an
exception."

Try moving the special Exception declaration (this line: public
MyClassInitException( string message ) : base( message ) { } ) into
the constructor. Since the class is not instantiated yet when the
error occurs, the fields of the class are not yet ready to use.
 
Yeah, I was typing from memory. MyClassSingleton and Instance are both
static.

I like your idea of pushing the "singleton" into the class itself ... I'm
going to try it out.
 
Yeah, I was typing from memory. MyClassSingleton and Instance are both
static.

I like your idea of pushing the "singleton" into the class itself ... I'm
going to try it out.
 
Back
Top