且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这个异常处理代码是否有效

更新时间:2023-12-05 11:32:52

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

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.