异常未处理 - 重新抛出异常
我尝试重新抛出异常,但它不起作用。 我在 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;
}
}
该方法属于类库,我从控制台应用程序调用它。 请帮我。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它正在按预期工作。
您捕获并重新抛出异常 - 您现在没有处理重新抛出的异常。这就是您收到错误的原因。
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.
捕获异常,进行一些处理,然后抛出异常的目的是使其可被堆栈中代码上方某处的某些 catch 语句捕获。如果异常没有在任何地方被捕获,它将上升到 CLR 级别并停止进程。
如果您想捕获异常,处理它,然后继续,很简单:只需不要将其扔回 catch 语句即可。
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.