繁体   English   中英

为什么不能像在循环中那样用分号来遵循Java中的catch子句?

[英]Why can't you follow a catch clause in Java with a semi-colon the same way you can for a loop?

以与可以在if语句,while循环,for循环中执行一行的方式相同,如下所示:

for(int i = 0; i < 10; someint += i++);

if(true);

while(waitForSomething());

为什么试一下就不能做同样的事情? 如:

try{
    int a = 1, b = 0;
    int c = a/b;
} catch (Exception e);

因为catch子句必须具有一个Block,所以forif语句仅需要一个Statement。

for(int i = 0; i < 10; someint += i++);

if(true);

其实就是

for(int i = 0; i < 10; someint += i++)
    ;

if(true)
    ;

参见JLS 14.9 if语句

 IfThenStatement: if ( Expression ) Statement 

JLS 14.20比较try语句

 TryStatement: try Block Catches Catches: CatchClause Catches CatchClause CatchClause: catch ( CatchFormalParameter ) Block 

查看statement ifStatement结尾,
try语句以Block结尾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM