2011-07-21 87 views
2

我尝试重新抛出异常,但它不起作用。 我在Visual Studio中得到'Exception was unhandled'错误。异常未处理 - 重新抛出异常

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; 
    } 
} 

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

谢谢。

+0

为什么在抓到它之后抛出异常?请发布您的实际代码。我怀疑你记录文本“...”,因为这没有意义。 –

回答

8

它按预期工作。

您捕获并重新抛出异常 - 您现在没有处理重新抛出的异常。这就是为什么你会收到错误。

+0

哦... :)谢谢!我无意识。谢谢! – ngi

0

捕获,做一些处理,然后抛出异常的要点是使它可用于捕获栈中代码上方某处的catch语句。如果这个异常没有捕获到任何地方,它将升级到CLR级别并停止这个过程。

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