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

前端 未结 7 1499
北海茫月
北海茫月 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.

    0 讨论(0)
提交回复
热议问题