uni-catch的异常参数可能是最终的? [英] exception parameter of uni-catch may be effectively final?

查看:1110
本文介绍了uni-catch的异常参数可能是最终的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Java的声明doc。

uni-catch子句的异常参数永远不会被隐式声明为final,但是 可能 有效最终。

可能是什么 暗示在这里。请举例解释。

What does may be implies here. Please explain with example.

推荐答案

JLS8在 4.12.4


局部变量或方法,构造函数,lambda或异常参数实际上是最终的,如果它没有被声明为final,但它永远不会作为赋值运算符的左手操作数(§15.26)或as前缀或后缀增量或减量运算符的操作数(§15.14,§15.15)。

A local variable or a method, constructor, lambda, or exception parameter is effectively final if it is not declared final but it never occurs as the left hand operand of an assignment operator (§15.26) or as the operand of a prefix or postfix increment or decrement operator (§15.14, §15.15).

在以下示例中,变量 e 有效的最终。这意味着它可用于 lambda表达式匿名内部类

In the following example, the variable e is effective final. That means it can be used in lambda expressions and anonymous inner classes:

try {
    throw new RuntimeException("foobar");
} catch (RuntimeException e) {
    Runnable r = () -> { System.out.println(e); };
    r.run();
}

在以下示例中,变量 e 有效最终,因为该变量已分配。这意味着,它不能在 lambda表达式匿名内部类中使用:

In the following example, the variable e is not effective final, because there is an assignment to that variable. That means, it can not be used within lambda expressions and anonymous inner classes:

try {
    throw new RuntimeException("foo");
} catch (RuntimeException e) {
    e = new RuntimeException("bar", e);
    Runnable r = () -> { System.out.println(e); }; // ERRROR
    r.run();
}

这篇关于uni-catch的异常参数可能是最终的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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