java中finally有什么意义呢,在现实中?

马克-to-win: finally有什么意义呢,在现实中?比如你开了一个流处理文件,可能没开成功,或开成功了,但后面的操作失败了,但不管你怎么样,你必须在一个地儿把它关闭,那就是finally块儿。
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
例: 1.10.1(try catch全齐,正常情况)-本章源码

public class Test {
    public static void main(String[] args) {
        try
        {
            int userInput=0;
            int I = 6 / userInput;
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            System.out.println("in finally");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}

输出结果:

java.lang.ArithmeticException: / by zero
in finally
马克-to-win:优雅结束




例: 1.10.2(异常没有发生)-本章源码

public class Test {
    public static void main(String[] args) {
        try
        {
            int userInput=2;
            int I = 6 / userInput;
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
      {
            System.out.println("in finally");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}
输出结果:
in finally
马克-to-win:优雅结束




例: 1.10.3(根本没有写catch块儿)-本章源码

public class Test {
    public static void main(String[] args) {
        try
        {
            int userInput=2;
            int I = 6 / userInput;
        }
        finally
        {
            System.out.println("in finally");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}

输出结果:

in finally
马克-to-win:优雅结束
例: 1.10.4(没捕获着异常,程序崩溃了)-本章源码

public class Test {
    public static void main(String[] args) {
        try {
            String str = null;
            str = str.toUpperCase();//本应捕获空指针异常
        } catch (ArithmeticException e) {//这里却想捕获数学异常, 完全捕不到
            System.out.println(e);
        }
        finally {//finally一定会被执行
            System.out.println("here is in final block.");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}
输出结果:
here is in final block.
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:5)




例: 1.10.5(try代码中有throw或直接崩溃)-本章源码

public class Test {
    static void subRMark_to_win() {
        try {
            System.out.println("子程序");
            int a=1/0;
        }
        finally {
            System.out.println("子程序的finally");
        }
    }
    public static void main(String args[]) {
        try {
            subRMark_to_win();
        } catch (Exception e) {
            System.out.println("主程序的catch");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}

输出结果:

子程序
子程序的finally
主程序的catch
马克-to-win:优雅结束


例: 1.10.6(try代码中有throw或直接崩溃)-本章源码

public class Test {
    static void subRMark_to_win() {
        try {
            System.out.println("子程序");
            throw new RuntimeException();
        }
        finally {
            System.out.println("子程序的finally");
        }
    }
    public static void main(String args[]) {
        try {
            subRMark_to_win();
        } catch (Exception e) {
            System.out.println("主程序的catch");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}
输出结果:
子程序
子程序的finally
主程序的catch
马克-to-win:优雅结束




例: 1.10.6_a(try代码中有OutOfMemoryError,依旧执行finally)

import java.util.Vector;

class Mark_to_win {
    long data;
}
public class Test {
    static Vector v = new Vector(10);
    static void subRMark_to_win() {
        try {
            System.out.println("子程序");
            int size_Make_to_win = (int) (Runtime.getRuntime().maxMemory() * 0.8);
            for (int i = 1; i < size_Make_to_win; i++) {
                Mark_to_win m = new Mark_to_win();
                v.add(m);
                m = null;
            }
        }
        finally {
            System.out.println("子程序的finally");
        }
    }
    public static void main(String args[]) {
        try {
            subRMark_to_win();
        } catch (Exception e) {
            System.out.println("主程序的catch");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}

输出:

子程序
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.Vector.grow(Unknown Source)
    at java.util.Vector.ensureCapacityHelper(Unknown Source)
    at java.util.Vector.add(Unknown Source)
    at com.Test.subRMark_to_win(Test.java:16)
    at com.Test.main(Test.java:26)
子程序的finally




例: 1.10.7(try代码中有return)-本章源码

public class Test {
    static void subRMark_to_win() {
        try {
            System.out.println("子程序");
            return;
        }
        finally {
            System.out.println("子程序的finally");
        }
    }
    public static void main(String args[]) {
        try {
            subRMark_to_win();
        } catch (Exception e) {
            System.out.println("主程序的catch");
        }
        System.out.println("马克-to-win:优雅结束");
    }
}
输出结果:
子程序
子程序的finally
马克-to-win:优雅结束