Java interview question: try catch finally return value question

Let's look at some codes and think about the return value we get in these codes.

First, return the basic type:

    public static int tryCatchTest1(){
        int a = 10;
        try{
            int i = 1/0;
        }catch(Exception e){
            return a;
        }finally{
            a = 20;
        }
        return 0;
    }

Second: return reference type

	public static int tryCatchTest2(){
        Person person = new Person();
        p.age = 18;
        try{
            int i = 1/0;
        }catch(Exception e){
            return p;
        }finally{
            p.age = 28;
        }
        return 0;
    }

Third: return specific values:

	public static int tryCatchTest3(){
        int a = 10;
        try{
            int i = 1/0;
        }catch(Exception e){
            return a;
        }finally{
            return 2;
        }
        return 0;
    }

First, we need to know the return execution process in try / catch / finally:

Next, we discuss these three cases respectively

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>