简体   繁体   中英

Exception was unhandled - rethrow exception

I try to rethrow an exception, but it's not working. I get 'Exception was unhandled' error in visual studio.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM