Java学习----finally块

public class Test {
    
    String x;
    public static void main(String[] args) {
        
        Test test = new Test();
        try {
            System.out.println(5/0);
        } catch (ArrayIndexOutOfBoundsException e) {
            test.x = "hello world";
            System.out.println(test.x.length());
        } catch (NullPointerException e) {
            // TODO: handle exception
            test.x = "hello world";
            System.out.println(test.x.length());
        } catch (Exception e) { // 必须放到最后
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            System.out.println("finally");
        }
        
        System.out.println("end");
    }
}
java.lang.ArithmeticException: / by zero
    at com.demo.pkg5.Test.main(Test.java:10)
finally
end
原文地址:https://www.cnblogs.com/dragon1013/p/5137388.html