这个异常处理代码是否有效 [英] is this exception handling code valid

查看:130
本文介绍了这个异常处理代码是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个区块是否会被击中?

Will both catch blocks be hit?

try
{
    DoSomething();
}
catch (AuthenticationException e)
{
    throw;
}
catch (Exception e)
{
    throw new AuthenticationException("Hello ");
}


推荐答案

它是有效的将编译,构建和运行),但这不是很好的做法。

It's valid (in that it will compile, build and run), but it's not good practice.

只有重新启动特定异常才能捕获一般异常会导致问题。如果没有其他的东西摧毁原始的异常调用堆栈。

Catching the general exception only to rethrow a specific exception will cause problems. If nothing else you are destroying the original exception call stack.

为了响应你的编辑,两个catch块都不会被击中。

In response to your edit, both catch blocks won't be hit.

如果 AuthenticationException 被提升,第一个块将被执行,对于任何其他异常,第二个块将被执行。

If AuthenticationException is raised the first block will be executed, for any other exception the second block will be executed.

这篇关于这个异常处理代码是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆