{if (true) return left;}怎么解释

chnliujd 2016-12-10 12:07:23
读activemq源代码 SelectorParser.java时见到如下函数

final public Expression orExpression() throws ParseException {
Expression left;
Expression right;
left = andExpression();
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case OR:
;
break;
default:
break label_1;
}
jj_consume_token(OR);
right = andExpression();
left = LogicExpression.createOR(asBooleanExpression(left), asBooleanExpression(right));
}
{if (true) return left;}
throw new Error("Missing return statement in function");
}

最后两行是什么意思?
...全文
861 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yqj2065 2016-12-31
  • 打赏
  • 举报
回复
有返回值的函数,需要抛出异常时,经常如此。
    private static int trouble(int j) throws ArithmeticException{
        int i =10/0;//10/j
        if(true) return 0;
        throw new ArithmeticException ("from trouble()");
    }
    public static void foo(){
        try{
            pln(trouble(1)); 
        }catch(ArithmeticException e){
            pln("trouble"); ;
        }
    }
trouble(int j)在正常工作如10/j时,返回其值。代码中直接给异常。
chnliujd 2016-12-28
  • 打赏
  • 举报
回复
最近读源码发现这个类是使用语法词法分析器自动编写的代码,不过最后2行还是没搞清楚具体的意思
菜 头 2016-12-28
  • 打赏
  • 举报
回复
{if (true) return left;} throw new Error("Missing return statement in function"); 如果不这么写,后面throw编译出错,
有个安河桥 2016-12-13
  • 打赏
  • 举报
回复
返回 left
NewshiJ 2016-12-13
  • 打赏
  • 举报
回复
if(true) return left 那不是一定返回left
爱睡觉的阿狸 2016-12-13
  • 打赏
  • 举报
回复
好神奇的写法,膜拜一下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧