Strange finally behaviour?

前端 未结 7 1264
野性不改
野性不改 2020-12-03 11:12
public class Test2 {

    public static void main(String[] args) {
        Test2 obj=new Test2();
        String a=obj.go();

        System.out.print(a);
    }


           


        
7条回答
  •  不知归路
    2020-12-03 11:44

    What is finally block?

    -By definition from Java "The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs."

    So, it prints "finally value of q is hello" as soon as it exists the try block and goes to line System.out.print(a); and prints the value returned by method go().

    If you have a debuggers like netbeans or eclipse, it can be analyzed by keeping the break point and waking through the code.

提交回复
热议问题