I try to rethrow an exception, but it's not working. I get 'Exception was unhandled' error in visual studio开发者_StackOverflow中文版.
public KeyValueConfigurationCollection getMyAppSetting()
{
Configuration config;
ConfigurationFileMap configFile;
try
{
configFile = new ConfigurationFileMap(ConfigurationManager.OpenMachineConfiguration().FilePath);
config = ConfigurationManager.OpenMappedMachineConfiguration(configFile);
AppSettingsSection MyAppSettingSection = (AppSettingsSection)config.GetSection("xxx/appSettings");
MyAppSettingSection.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToRoamingUser;
return MyAppSettingSection.Settings;
}
catch (Exception ex)
{
logger.Fatal("...");
throw;
}
}
This method belong to a class library, and i call it from a console application. Please, help me.
Thanks.
It is working as expected.
You catch, then rethrow the exception - you are now not handling the rethrown exception. That's why you are getting the error.
The point of catching, doing some treatments, then throwing back the exception is to make it available for catching by some catch statement somewhere above your code in the stack. If the exception is not catched anywhere, it will go up to the CLR level and stop the process.
If you want to catch the exception, handle it, then move on, it's simple: just don't throw it back in the catch statement.
精彩评论