How to return a value from try, catch, and finally?

前端 未结 4 913
情歌与酒
情歌与酒 2020-12-18 18:30

So when I do a code of blocks inside a try{}, and I try to return a value, it tells me

no return values

4条回答
  •  北海茫月
    2020-12-18 19:31

    Here is another example that return's a boolean value using try/catch.

    private boolean doSomeThing(int index){
        try {
            if(index%2==0) 
                return true; 
        } catch (Exception e) {
            System.out.println(e.getMessage()); 
        }finally {
            System.out.println("Finally!!! ;) ");
        }
        return false; 
    }
    

提交回复
热议问题