[英]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,所以for
和if
语句仅需要一个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
TryStatement: try Block Catches Catches: CatchClause Catches CatchClause CatchClause: catch ( CatchFormalParameter ) Block
查看statement if
以Statement
结尾,
而try
语句以Block
结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.