Java try catch finally 异常 分析

版权声明:本文为HCG原创文章,未经博主允许不得转载。请联系[email protected] https://blog.csdn.net/qq_39455116/article/details/87880574
  public static int method_1() {
        int a = 1;
        try {
         System.out.println("try");
            return a;
        } catch (Exception e) {
            System.out.println("11");
        } finally {
       	   System.out.println("finally");
            ++a;
            return a;

        }

    }

答案:

try
finally
2
即没有异常的情况,先执行try,最后执行finally


public static int method_2() {
        int a = 1;
        try {
            System.out.println("try");
            int  u=1/0;
            System.out.println("exception");
            return a;
        } catch (Exception e) {
            System.out.println("catch");
        } finally {
            System.out.println("finally");
            ++a;
            return a;
        }

    }

答案:

try
catch
finally
2
有异常,先捕获,最后finally


  public static int method_3() {
        int a = 1;
        try {
            System.out.println("try");
            int  u=1/0;
            System.out.println("exception");
            return a;
        } catch (  Exception e) {
            System.out.println("catch");
            return a;
        } finally {
            System.out.println("finally");
            ++a;

        }

    }

答案:

try
catch
finally
1
方法3和方法2主要的区别在于是catch里面返回还是finally里面返回
当其中只要一个返回时,最后执行其中的返回,但是finally一定会执行


 public static int method_4() {
        int a = 1;
        try {
            System.out.println("try");
            int  u=1/0;
            System.out.println("exception");
            return a;
        } catch (  Exception e) {
            System.out.println("catch");
            return a;
        } finally {
            System.out.println("finally");
            ++a;
            return a;
        }
    }

答案:

try
catch
finally
2
当catch和finally都有返回的时候,执行finally返回


  public static int method_5() {
        int a = 1;
        try {
            System.out.println("try");
            return a;
        } catch (Exception e) {
            System.out.println("catch");

        } finally {
            System.out.println("finally");
            ++a;
            System.out.println("finally" + a);
        }
        return a;
    }

答案:

try
finally
finally2
1
当没有异常的时候,执行finally,但是其中计算不计入结果


多个catch

  public static int method_6() {
        int a = 1;
        try {
            System.out.println("try");
            int  u=1/0;
            return a;
        } catch (  ArithmeticException e) {
            System.out.println("ArithmeticException");
            return a+1;
        } catch (Exception e) {
            System.out.println("Exception");
            return a;
        }finally {
            System.out.println("finally");
            ++a;

        }

    }

答案:

try
ArithmeticException
finally
2


 public static int method_7() {
        int a = 1;
        try {
            System.out.println("try");
            int  u=1/0;
            return a;
        } catch (  NullPointerException  e) {
            System.out.println("NullPointerException ");
            return a+1;
        } catch (Exception e) {
            System.out.println("Exception");
            return a;
        }finally {
            System.out.println("finally");
            ++a;

        }

    }

答案:

try
Exception
finally
1

总结

1/0是 ArithmeticException 异常,而非空指针

1. 多个异常,谁第一个捕获了最精确的异常,谁输出
2.  第一个catch没有捕获到异常,由第二个接着捕获

猜你喜欢

转载自blog.csdn.net/qq_39455116/article/details/87880574