VS .NET 2003 CF
In this method I cannot catch exceptions thrown by Settings constructor (for
example FileNotFoundException) but only an OutOfMemory exception due to Get
returning void.
Why???
private void GtmForm_Load(object sender, System.EventArgs e)
{
try
{
this.idLength = uint.Parse(Settings.Get("idLength"));
}
catch(FileNotFoundException fnfEx)
{
MessageBox.Show(fnfEx.Message);
Application.Exit();
}
catch(XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message + "\n" +
xmlEx.InnerException.Message);
Application.Exit();
}
catch(Exception ex)
{
MessageBox.Show("Error loading configuration file!\n" + ex.Message);
Application.Exit();
}
this.employeeAssetIdTextBox.Focus();
}
public class Settings
{
private static readonly string path;
private static readonly NameValueCollection appSettings;
static Settings()
{
path =
Path.GetDirectoryName(System.Reflection.Assembly.G etExecutingAssembly().GetName().CodeBase);
path += @"\Setting.xml";
if (!File.Exists(path))
{
throw new FileNotFoundException(path + " could not be found!");
}
appSettings = new NameValueCollection();
try
{
XmlTextReader appSettingsReader = new XmlTextReader(path);
while (appSettingsReader.Read())
{
if (appSettingsReader.NodeType == XmlNodeType.Element &&
appSettingsReader.AttributeCount == 2)
{
appSettings.Add(appSettingsReader.GetAttribute(0),
appSettingsReader.GetAttribute(1));
}
}
}
catch(XmlException e)
{
throw new XmlException("Error processing configuration
file!",e);
}
}
public static string Get(string key)
{
try
{
return appSettings.Get(key);
}
catch(FileNotFoundException)
{
throw new FileNotFoundException(path + " could not be found!");
}
catch(XmlException e)
{
throw new XmlException("Error processing configuration
file!",e);
}
}
}