【问题标题】:Exception was unhandled - rethrow exception异常未处理 - 重新抛出异常
【发布时间】:2011-10-10 07:59:35
【问题描述】:

我尝试重新抛出异常,但它不起作用。 我在 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;
  }
}

此方法属于类库,我从控制台应用程序调用它。 请帮我。

谢谢。

【问题讨论】:

  • 捕捉到异常后为什么还要抛出异常?请发布您的实际代码。我怀疑您记录文本“...”,因为这没有任何意义。

标签: c# .net exception console-application class-library


【解决方案1】:

它按预期工作。

您捕获,然后重新抛出异常 - 您现在没有处理重新抛出的异常。这就是您收到错误消息的原因。

【讨论】:

    【解决方案2】:

    捕获,进行一些处理,然后抛出异常的目的是使它可以被堆栈中代码上方的某个 catch 语句捕获。如果在任何地方都没有捕获到异常,它将上升到 CLR 级别并停止进程。

    如果你想捕获异常,处理它,然后继续,很简单:只是不要在 catch 语句中把它扔回去。

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 2011-12-26
      相关资源
      最近更新 更多