简体   繁体   English

异常未处理 - 重新抛出异常

[英]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.我在 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.此方法属于 class 库,我从控制台应用程序调用它。 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.捕获,进行一些处理,然后抛出异常的目的是使它可以被堆栈中代码上方的某个 catch 语句捕获。 If the exception is not catched anywhere, it will go up to the CLR level and stop the process.如果在任何地方都没有捕获到异常,它将 go 上升到 CLR 级别并停止进程。

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.如果你想捕获异常,处理它,然后继续,这很简单:只是不要把它扔回 catch 语句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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