理解'finally'块 [英] Understanding the 'finally' block

查看:106
本文介绍了理解'finally'块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了七个测试用例来理解 finally 块的行为。 最终的工作原理是什么?

I've written seven test cases for understanding the behavior of the finally block. What is the logic behind how finally works?

package core;

public class Test {
    public static void main(String[] args) {
        new Test().testFinally();
    }

    public void testFinally() {
        System.out.println("One = " + tryOne());
        System.out.println("Two = " + tryTwo());
        System.out.println("Three = " + tryThree());
        System.out.println("Four = " + tryFour());
        System.out.println("Five = " + tryFive());
        System.out.println("Six = " + trySix());
        System.out.println("Seven = " + trySeven());
    }

    protected StringBuilder tryOne() {
        StringBuilder builder = new StringBuilder();
        try {
            builder.append("Cool");
            return builder.append("Return");
        }
        finally {
            builder = null;
        }
    }

    protected String tryTwo() {
        String builder = "Cool";
        try {
            return builder += "Return";
        }
        finally {
            builder = null;
        }
    }

    protected int tryThree() {
        int builder = 99;
        try {
            return builder += 1;
        }
        finally {
            builder = 0;
        }
    }

    protected StringBuilder tryFour() {
        StringBuilder builder = new StringBuilder();
        try {
            builder.append("Cool");
            return builder.append("Return");
        }
        finally {
            builder.append("+1");
        }
    }

    protected int tryFive() {
        int count = 0;
        try {
            count = 99;
        }
        finally {
            count++;
        }
        return count;
    }

    protected int trySix() {
        int count = 0;
        try {
            count = 99;
        }
        finally {
            count = 1;
        }
        return count;
    }

    protected int trySeven() {
        int count = 0;
        try {
            count = 99;
            return count;
        }
        finally {
            count++;
        }
    }
}

为什么 builder = null 无效?

为什么 builder.append(+ 1)工作而 count ++ (在 trySeven ()中)工作吗?

Why does builder.append("+1") work whereas count++( in trySeven()) does not work?

推荐答案

执行返回后,覆盖它的唯一方法是执行另一次返回(如从Java中的finally块返回,这几乎总是一个坏主意),或者以其他方式突然完成。你的测试永远不会从最终返回。

Once you do the return, the only way to override that is to do another return (as discussed at Returning from a finally block in Java, this is almost always a bad idea), or otherwise complete abruptly. Your tests don't ever return from a finally.

JLS§ 14.1 定义了突然完成。突然完成类型之一是返回。由于返回,1,2,3,4和7中的try块突然完成。如§ 14.20.2 ,如果try块在抛出的原因R之外突然完成,则立即执行finally块。

JLS §14.1 defines abrupt completion. One of the abrupt completion types is a return. The try blocks in 1,2,3,4, and 7 abruptly complete due to returns. As explained by §14.20.2, if the try block completes abruptly for a reason R besides a throw, the finally block is immediately executed.

如果finally块正常完成(这意味着没有返回,除其他事项外,try语句因为R而突然完成。换句话说,尝试启动的返回保持不变;这适用于所有测试。如果你从finally返回,try语句因为原因S而突然完成(原因R被丢弃)。 (这是新的重要回报)。

If the finally block completes normally (which implies no return, among other things), "the try statement completes abruptly for reason R.". In other words, the return initiated by the try is left intact; this applies to all your tests. If you return from the finally, "the try statement completes abruptly for reason S (and reason R is discarded)." (S here being the new overriding return).

所以在tryOne中,如果你这样做:

So in tryOne, if you did:

finally {
            builder = null;
            return builder;
        }

这个新的回报S会覆盖原始回报R.

this new return S would override the original return R.

对于 builder.append(+ 1) tryFour 中,请保持记住StringBuilder是可变的,所以你仍然返回对try中指定的同一对象的引用。你只是做了最后一分钟的突变。

For builder.append("+1") in tryFour, keep in mind StringBuilder is mutable, so you're still returning a reference to the same object specified in the try. You're just doing a last minute mutation.

tryFive trySix 很简单。由于try中没有返回,try和finally都正常完成,并且执行方式就像没有try-finally一样。

tryFive and trySix are straight-forward. Since there is no return in the try, the try and finally both complete normally, and it executes the same as if there was no try-finally.

这篇关于理解'finally'块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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