finally in exception handling

后端 未结 8 1628
甜味超标
甜味超标 2021-01-18 17:08

What exactly does a finally block in exception handling perform?

8条回答
  •  北海茫月
    2021-01-18 17:50

    If you return a value in your try or catch block as well as in the finally block, keep in mind that the finally block's return value is what you'll end up with (the last block executed). That means if you try some code that does NOT throw an Exception and is supposed to return a value, but your finally block is also supposed to return a value, the finally block's value is what will actually be returned. This SO thread talks about that very point. I don't believe returning a value inside a try or catch is usually necessary or the best idea. Also note that System.exit(0) kills the JVM and thus halts execution before anything else runs, which might render your finally block unexecuted.

提交回复
热议问题