java - 该定义中的 "leftmost"是什么意思?

标签 java exception

JLS at the 14.20.1说:

If the run-time type of V is assignment compatible with (§5.2) a catchable exception class of any catch clause of the try statement, then the first (leftmost) such catch clause is selected.

最左边是什么?我们从上到下放置 catch 子句,例如:

try{ }
catch(IndexOutOfBoundException e){ }
catch(SQLException e){ }
catch(NullPointerException e){ }
//etc

最佳答案

这与 Java 的任何新语言特性无关。

术语“最左边”仅适用于最早出现的 catch 子句。将代码想象成一维标记序列,从左向右延伸。这就是语言解析器的看法。

因此,所有规定都是输入第一个匹配的 catch-clause。

关于java - 该定义中的 "leftmost"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178156/

相关文章:

python - 如何捕获 ctypes 中抛出的异常?

.net - 防止调试器中断第 3 方库中的所有异常

java - 如何截断 java.sql.Date 上的时间?

java - 在javascript中比较字符串(字符串来源来自xmlhttp.responseText)

java - 允许空指针异常广告克服它的最佳方法

java - 为什么 Python 异常被命名为 "Error"?

java - Spring、Tomcat7中的全局异常处理

java - Exception.getCause()可以用来知道链式异常吗?

java嵌套if语句else不起作用

java - 在Java中,Spring无需任何脚本即可维护数据库