Java Try Catch Final 没有 Catch 的情况下会阻塞

发布于 2024-10-09 19:54:39 字数 103 浏览 12 评论 0原文

我正在审查一些新代码。该程序只有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

怎会甘心 2024-10-16 19:54:40

如果是的话,try 块如何工作
遇到异常或者什么情况
可投掷

异常被抛出块外,就像任何其他未捕获异常的情况一样。

无论 try 块如何退出,finally 块都会执行——无论是否存在任何 catch,无论是否存在匹配的 catch。

catch 块和finally 是try 块的正交部分。您可以选择其中之一,也可以两者兼而有之。有了 Java 7,您将两者都不能拥有!

how does the try block work if it
encounters an exception or anything
throwable

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!

心清如水 2024-10-16 19:54:40

你不试试那个程序吗?它会转到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!

趁微风不噪 2024-10-16 19:54:40

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.

小兔几 2024-10-16 19:54:40

无论 try 块中是否抛出异常 - finally 块都会被执行。异常不会被捕获。

Regardless of exception thrown or not in try block - finally block will be executed. Exception would not be caught.

¢蛋碎的人ぎ生 2024-10-16 19:54:40

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.

池木 2024-10-16 19:54:39

如果 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.

南汐寒笙箫 2024-10-16 19:54:39

否则finally 将始终执行

  • 关于 try/finally 的一个小注意事项:除非调用 System.exit()
  • 。 JVM 崩溃。
  • try{} 块永远不会结束(例如无限循环)。

A small note on try/finally: The finally will always execute unless

  • System.exit() is called.
  • The JVM crashes.
  • The try{} block never ends (e.g. endless loop).
懒的傷心 2024-10-16 19:54:39

Java 语言规范(1) 描述了 try-catch-finally 的执行方式。
没有 catch 相当于没有 catch 能够捕获给定的 Throwable。

  • 如果 try 块的执行由于抛出值 V 而突然完成,那么有一个选择:
    • 如果 V 的运行时类型可分配给 try 语句的任何 catch 子句的参数,则 ...
    • 如果 V 的运行时类型不可分配给 try 语句的任何 catch 子句的参数,则执行 finally 块。然后就有一个选择:
      • 如果finally块正常完成,那么try语句会因为抛出值V而突然完成。
      • 如果finally 块由于原因S 突然完成,则try 语句也会由于原因S 突然完成(并且值V 的抛出被丢弃并被遗忘)。

(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.

  • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
    • If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then …
    • If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
      • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
      • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).

(1) Execution of try-catch-finally

朦胧时间 2024-10-16 19:54:39

内部finally 在将异常抛出到外部块之前执行。

public class TryCatchFinally {

  public static void main(String[] args) throws Exception {

    try{
        System.out.println('A');
        try{
            System.out.println('B');
            throw new Exception("threw exception in B");
        }
        finally
        {
            System.out.println('X');
        }
        //any code here in the first try block 
        //is unreachable if an exception occurs in the second try block
    }
    catch(Exception e)
    {
        System.out.println('Y');
    }
    finally
    {
        System.out.println('Z');
    }
  }
}

结果

A
B
X
Y
Z

The inner finally is executed prior to throwing the exception to the outer block.

public class TryCatchFinally {

  public static void main(String[] args) throws Exception {

    try{
        System.out.println('A');
        try{
            System.out.println('B');
            throw new Exception("threw exception in B");
        }
        finally
        {
            System.out.println('X');
        }
        //any code here in the first try block 
        //is unreachable if an exception occurs in the second try block
    }
    catch(Exception e)
    {
        System.out.println('Y');
    }
    finally
    {
        System.out.println('Z');
    }
  }
}

Results in

A
B
X
Y
Z
故人的歌 2024-10-16 19:54:39

finally 块总是在 try 块结束后运行,无论 try 正常结束还是由于异常(呃,可抛出)异常结束。

如果 try 块中的任何代码抛出异常,则当前方法只需重新抛出(或继续抛出)相同的异常(在运行 finally 块之后)。

如果finally块抛出异常/错误/可抛出异常,并且已经有一个待处理的可抛出异常,那么它会变得很难看。坦率地说,我完全忘记了发生了什么(几年前我的认证就这么多了)。我认为两个 throwables 都链接在一起,但是你必须做一些特殊的巫术(即 - 我必须查找的方法调用)才能在“最终”吐出之前得到原始问题,呃,吐了。

顺便说一句,try/finally 是资源管理中非常常见的事情,因为 java 没有析构函数。

例如 -

r = new LeakyThing();
try { useResource( r); }
finally { r.release(); }  // close, destroy, etc

“最后”,还有一个提示:如果您确实费心放入一个 catch,要么捕获特定的(预期的)可抛出子类,要么只捕获“Throwable”, > “异常”,用于一般的包罗万象的错误陷阱。太多的问题,例如反射错误,会抛出“错误”,而不是“异常”,而这些问题将被任何编码为“捕获所有”的代码所忽略:

catch ( Exception e) ...  // doesn't really catch *all*, eh?

改为执行以下操作:

catch ( Throwable t) ...

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. -

r = new LeakyThing();
try { useResource( r); }
finally { r.release(); }  // close, destroy, etc

"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:

catch ( Exception e) ...  // doesn't really catch *all*, eh?

do this instead:

catch ( Throwable t) ...
梨涡 2024-10-16 19:54:39

版本 7 之前的 Java 版本允许 try-catch-finally 这三种组合...

try - catch
try - catch - finally
try - finally

finally 块将始终执行,无论 中发生什么情况try 或/和 catch 块。因此,如果没有 catch 块,则此处不会处理异常。

但是,您仍然需要在代码中的某个位置设置异常处理程序 - 当然,除非您希望应用程序完全崩溃。这取决于应用程序的体系结构,该处理程序的确切位置。

  • Java try 块后面必须跟有 catch 或finally 块。
  • 对于每个 try 块,可以有零个或多个 catch 块,但只能有一个 finally 块。
  • 如果程序退出(通过调用 System.exit() 或导致致命错误导致进程中止),finally 块将不会被执行。

Java versions before version 7 allow for these three combinations of try-catch-finally...

try - catch
try - catch - finally
try - finally

finally block will be always executed no matter of what's going on in the try or/and catch block. so if there is no catch 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.

  • Java try block must be followed by either catch or finally block.
  • For each try block there can be zero or more catch blocks, but only one finally block.
  • The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文