How does return work in try, catch, finally in Java?

前端 未结 7 1762
北海茫月
北海茫月 2021-02-01 16:26

I can\'t understand exactly how return works in try, catch.

  • If I have try and finally without
7条回答
  •  无人共我
    2021-02-01 17:26

    In the second example, if the checked exception occurs then it hands over to calling a method.

    In the first example, if the checked exception occurs then it handles in the same method, as catch block take the responsibility to handle the exception.

    if you write return statement in catch block then it works.
    i.e,

    try{  
        return ..  
    }catch(Exception e){  
        return ..  
    }finally{  
    }
    

    But it is not good programming practice.

提交回复
热议问题