Java Try Catch Final 没有 Catch 的情况下会阻塞
我正在审查一些新代码。该程序只有一个 try 和一个 finally 块。既然排除了 catch 块,那么如果 try 块遇到异常或任何可抛出的内容,它如何工作?它直接进入finally块吗?
I'm reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
异常被抛出块外,就像任何其他未捕获异常的情况一样。
无论 try 块如何退出,finally 块都会执行——无论是否存在任何 catch,无论是否存在匹配的 catch。
catch 块和finally 是try 块的正交部分。您可以选择其中之一,也可以两者兼而有之。有了 Java 7,您将两者都不能拥有!
The exception is thrown out of the block, just as in any other case where it's not caught.
The finally block is executed regardless of how the try block is exited -- regardless whether there are any catches at all, regardless of whether there is a matching catch.
The catch blocks and the finally are orthogonal parts of the try block. You can have either or both. With Java 7, you'll be able to have neither!
你不试试那个程序吗?它会转到finally块并执行finally块,但是,异常不会被处理。但是,这个异常可以在finally块中被推翻!
Don't you try it with that program? It'll goto finally block and executing the finally block, but, the exception won't be handled. But, that exception can be overruled in the finally block!
finally 块在 try 块完成后执行。如果在 try 块离开时将某些东西扔到 try 块内,则执行 finally 块。
The finally block is executed after the try block completes. If something is thrown inside the try block when it leaves the finally block is executed.
无论
try
块中是否抛出异常 -finally
块都会被执行。异常不会被捕获。Regardless of exception thrown or not in
try
block -finally
block will be executed. Exception would not be caught.在
try
块内,我们编写可以引发异常的代码。catch
块是我们处理异常的地方。无论是否发生异常,
finally
块都会被执行。现在,如果我们有 try-finally 块而不是 try-catch-finally 块,那么异常将不会被处理,并且在 try 块之后而不是控制去 catch 块,它将转到finally 块。
当我们不想对异常执行任何操作时,可以使用 try-finally 块。
Inside
try
block we write codes that can throw an exception.The
catch
block is where we handle the exception.The
finally
block is always executed no matter whether exception occurs or not.Now if we have try-finally block instead of try-catch-finally block then the exception will not be handled and after the try block instead of control going to catch block it will go to finally block.
We can use try-finally block when we want to do nothing with the exception.
如果 try 块中的任何代码可以引发已检查异常,则它必须出现在方法签名的 throws 子句中。如果抛出未经检查的异常,它将从方法中冒泡出来。
无论是否抛出异常,finally 块都会被执行。
If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.
The finally block is always executed, whether an exception is thrown or not.
否则finally 将始终执行
try
/finally
的一个小注意事项:除非调用System.exit()
,try{}
块永远不会结束(例如无限循环)。A small note on
try
/finally
: The finally will always execute unlessSystem.exit()
is called.try{}
block never ends (e.g. endless loop).Java 语言规范(1) 描述了
try-catch-finally
的执行方式。没有 catch 相当于没有 catch 能够捕获给定的 Throwable。
(1) 执行try-catch-finally
The Java Language Specification(1) describes how
try-catch-finally
is executed.Having no catch is equivalent to not having a catch able to catch the given Throwable.
(1) Execution of try-catch-finally
内部finally 在将异常抛出到外部块之前执行。
结果
The inner finally is executed prior to throwing the exception to the outer block.
Results in
finally 块总是在 try 块结束后运行,无论 try 正常结束还是由于异常(呃,可抛出)异常结束。
如果 try 块中的任何代码抛出异常,则当前方法只需重新抛出(或继续抛出)相同的异常(在运行 finally 块之后)。
如果finally块抛出异常/错误/可抛出异常,并且已经有一个待处理的可抛出异常,那么它会变得很难看。坦率地说,我完全忘记了发生了什么(几年前我的认证就这么多了)。我认为两个 throwables 都链接在一起,但是你必须做一些特殊的巫术(即 - 我必须查找的方法调用)才能在“最终”吐出之前得到原始问题,呃,吐了。
顺便说一句,try/finally 是资源管理中非常常见的事情,因为 java 没有析构函数。
例如 -
“最后”,还有一个提示:如果您确实费心放入一个 catch,要么捕获特定的(预期的)可抛出子类,要么只捕获“Throwable”,不 > “异常”,用于一般的包罗万象的错误陷阱。太多的问题,例如反射错误,会抛出“错误”,而不是“异常”,而这些问题将被任何编码为“捕获所有”的代码所忽略:
改为执行以下操作:
The finally block is always run after the try block ends, whether try ends normally or abnormally due to an exception, er, throwable.
If an exception is thrown by any of the code within the try block, then the current method simply re-throws (or continues to throw) the same exception (after running the finally block).
If the finally block throws an exception / error / throwable, and there is already a pending throwable, it gets ugly. Quite frankly, I forget exactly what happens (so much for my certification years ago). I think both throwables get linked together, but there is some special voodoo you have to do (i.e. - a method call I would have to look up) to get the original problem before the "finally" barfed, er, threw up.
Incidentally, try/finally is a pretty common thing to do for resource management, since java has no destructors.
E.g. -
"Finally", one more tip: if you do bother to put in a catch, either catch specific (expected) throwable subclasses, or just catch "Throwable", not "Exception", for a general catch-all error trap. Too many problems, such as reflection goofs, throw "Errors", rather than "Exceptions", and those will slip right by any "catch all" coded as:
do this instead:
版本 7 之前的 Java 版本允许 try-catch-finally 这三种组合...
finally
块将始终执行,无论中发生什么情况try
或/和catch
块。因此,如果没有catch
块,则此处不会处理异常。但是,您仍然需要在代码中的某个位置设置异常处理程序 - 当然,除非您希望应用程序完全崩溃。这取决于应用程序的体系结构,该处理程序的确切位置。
Java versions before version 7 allow for these three combinations of try-catch-finally...
finally
block will be always executed no matter of what's going on in thetry
or/andcatch
block. so if there is nocatch
block, the exception won't be handled here.However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.