Behaviour of return statement in catch and finally

后端 未结 8 812
情歌与酒
情歌与酒 2020-11-27 11:44

Please see the following code and explain the output behavior.

public class MyFinalTest {

    public int doMethod(){
        try{
            throw new Exce         


        
8条回答
  •  北海茫月
    2020-11-27 12:18

    finally is always executed (the only exception is System.exit()). You can think of this behavior this way:

    1. An exception is thrown
    2. Exception is caught and return value is set to 5
    3. Finally block gets executed and return value is set to 10
    4. The function returns

提交回复
热议问题