singleton not bubbling-up constructor exception

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."
 
H

harborsparrow

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.
 
H

harborsparrow

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.
 
J

John A Grandy

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.
 
J

John A Grandy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top