Exception from static method

S

Saurabh

Hi All,

I have an application which has certain preferences stored in a file. The
general architecture of reading these preferences is, There is a preferences
accessor class (Preferences) which has 2 static methods GetValue and
SetValue. My application accesses the preferences through this class using
the static method. The class has a static constructor where I read the file
and load the preferences into a hashtable. If the preferences file is not
found I throw an exception of type MyException derived from application
exception, which is caught by the first caller. so the calls are

//My application
try
{
string str = Preferences.GetValue("PreferredColor");
}
catch (MyApplicationException mae)
{
MessageBox.Show(mae.Message)
}

//Preferences class
public class Preferences
{
private static Hashtable _ht
static Preferences()
{
_ht = new Hashtable
LoadPreferences();
}

internal static LoadPreferences()
{
//code to read the file and load into the hashtable
if (File.Exist(<myfile>)
{
//read it
}
else
{
throw new MyApplicationException( );
}
}

public static GetValue(string key)
{
return _ht[key];
}
}

Now if the file is missing, I throw the exception but before reaching the
place where I catch the exception, the IDE exception handler kicks in and
shows the message and the control does not reach at all in my catch block.

Does anybody have a clue as to what is happening ?

--Saurabh
 
S

Saurabh

Further to the previous post, If I change the static method to nonstatic and
call the GetValue on instances, I can catch the exception.

Does anybody know why this happens ?

TIA,

--Saurabh
 
J

Jon Skeet [C# MVP]

Saurabh said:
I have an application which has certain preferences stored in a file. The
general architecture of reading these preferences is, There is a preferences
accessor class (Preferences) which has 2 static methods GetValue and
SetValue. My application accesses the preferences through this class using
the static method. The class has a static constructor where I read the file
and load the preferences into a hashtable. If the preferences file is not
found I throw an exception of type MyException derived from application
exception, which is caught by the first caller. so the calls are

Now if the file is missing, I throw the exception but before reaching the
place where I catch the exception, the IDE exception handler kicks in and
shows the message and the control does not reach at all in my catch block.

Does anybody have a clue as to what is happening ?

Yup. When an exception is thrown by a type initializer (like your
static constructor), it is wrapped by a TypeInitializationException,
not thrown directly. In other words, the part of your description where
you say "which is caught by the first caller" isn't true.
 
S

Saurabh

Hi Jon,

Thanks very much for the quick response, Much appreciated.

Is my approach not correct or should I be catching the
TypeInitializationException as well? I would really want to have these
static methods so that the rest of application can access the preferences in
a much simpler way.

Any thoughts around this?

--Saurabh
 
J

Jon Skeet [C# MVP]

Saurabh said:
Thanks very much for the quick response, Much appreciated.

Is my approach not correct or should I be catching the
TypeInitializationException as well? I would really want to have these
static methods so that the rest of application can access the preferences in
a much simpler way.

Any thoughts around this?

One approach I've seen taken elsewhere is to stash the exception rather
than throwing it, and then for each static method, check whether or not
there was an exception stashed, and throw it if so. e.g.

using System;

class MyClass
{
static MyClass
{
try
{
// Do something here
}
catch (Exception e)
{
startupException = e;
}
}

static Exception startupException;

static string GetConfig(string name)
{
CheckStartup();
return ...;
}

static void SetConfig (string name, string value)
{
CheckStartup();
...
}

static void CheckStartup()
{
if (startupException != null)
{
throw startupException;
}
}
}
 
S

Saurabh

It will work and will solve the basic problem, will try and give it a shot.
Thanks a lot for your inputs.

--Saurabh
 

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