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

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

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

  • If I have try and finally without
7条回答
  •  -上瘾入骨i
    2021-02-01 17:22

    I think this is what you're asking:

    And if I have try, catch, finally I can't put return in the try block.

    So if you add a catch block, you can't put a return in the try block.

    The problem is that if you add a catch then control drops through and you need a return at the end of the method or it's a syntax error. I haven't tested this but I assume you could put a return in the try block, but you would also have to add one inside the catch or at then end of the method as you have now.

提交回复
热议问题