本文共 6640 字,大约阅读时间需要 22 分钟。
try, catch, finally
throws
public class Test { public static void main (String args[]) { int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = div(m, n); System.out.println("m/n=" + r); } public static int div(int m, int n) { int r = 0; r= m/n; return r; }}
执行 java Test 9 0会抛出算数异常,那怎么捕捉呢?
public class Test { public static void main (String args[]) { int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = div(m, n); System.out.println("m/n=" + r); } public static int div(int m, int n) { int r = 0; try { r = m/n; } catch (ArithmeticException e) { System.out.println(e); } return r; }}
执行结果
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0java.lang.ArithmeticException: / by zerom/n=0
finally就是不管是否发生异常,都会最终执行到的语句!
加上finally看一下效果!
public static int div(int m, int n) { int r = 0; try { r = m/n; } catch (ArithmeticException e) { System.out.println(e); } finally { System.out.println("this is finally!"); } return r; }
执行的结果,9/1, 或者9/0最终都会执行finally的语句
lydia@lydia:~/sgy/java_learn/exception$ javac Test.javalydia@lydia:~/sgy/java_learn/exception$ java Test 9 1this is finally!m/n=9lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0java.lang.ArithmeticException: / by zerothis is finally!m/n=0
例如div函数自己本身不处理,扔给main方法
public class Test { public static void main (String args[]) { int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = 0; try { r = div(m, n); } catch (ArithmeticException e) { System.out.println(e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws ArithmeticException { int r = 0; r = m / n; return r; }}
现在的程序只会在div中处理这个异常!
Test.javapublic class Test { public static void main (String args[]) { int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = 0; try { r = div(m, n); } catch (ArithmeticException e) { System.out.println("main-->"+e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws ArithmeticException { int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); } return r; }}
执行的结果只有div会处理这个异常
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0div--->java.lang.ArithmeticException: / by zerom/n=0
需要通过手动抛出异常!
修改后的代码!
public static int div(int m, int n) throws ArithmeticException { int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); throw e; } return r; }
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.ArithmeticException: / by zerom/n=0
Test.java
public class Test { public static void main (String args[]) { int m = 0; int n =0; int r = 0; try { m = Integer.parseInt(args[0]); n = Integer.parseInt(args[1]); r = div(m, n); } catch (ArithmeticException e) { System.out.println("main-->"+e); } catch (RuntimeException e) { System.out.println("main-->"+e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws ArithmeticException { int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); throw e; } return r; }}
执行结果
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.ArithmeticException: / by zerom/n=0lydia@lydia:~/sgy/java_learn/exception$ java Test 9 amain-->java.lang.NumberFormatException: For input string: "a"m/n=0lydia@lydia:~/sgy/java_learn/exception$ java Test 9main-->java.lang.ArrayIndexOutOfBoundsException: 1m/n=0
Test.java
div函数改成下面这样!public static int div(int m, int n) { int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); throw new Exception("My error!"); } return r; }
编译不通过,new Execption 必须处理或者抛出!
lydia@lydia:~/sgy/java_learn/exception$ javac Test.javaTest.java:27: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出 throw new Exception("My error!"); ^1 个错误
继续更改,div函数
public static int div(int m, int n) throws Exception{ int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); throw new Exception("My error!"); } return r; }
编译结果还是一样,Main函数里面也需要处理!
lydia@lydia:~/sgy/java_learn/exception$ javac Test.javaTest.java:11: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出 r = div(m, n); ^1 个错误
对Main方法增加 catch Exception,处理这个异常,就不会有问题
public class Test { public static void main (String args[]) { int m = 0; int n =0; int r = 0; try { m = Integer.parseInt(args[0]); n = Integer.parseInt(args[1]); r = div(m, n); } catch (ArithmeticException e) { System.out.println("main-->"+e); } catch (RuntimeException e) { System.out.println("main-->"+e); } catch (Exception e) { System.out.println("main-->"+e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws Exception{ int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); throw new Exception("My error!"); } return r; }}
执行的结果
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.Exception: My error!m/n=0
或者直接在div当中处理掉!
在div中增加try catch的语句,直接本方法处理!public static int div(int m, int n) throws Exception{ int r = 0; try { r = m / n; } catch (ArithmeticException e) { System.out.println("div--->"+ e); try { throw new Exception("My error!"); } catch (Exception e1) { System.out.println("div-->"+e1); } } return r; }
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0div--->java.lang.ArithmeticException: / by zerodiv-->java.lang.Exception: My error!m/n=0
try或catch块中有return或throw语句,会先执行finally块,再返回来执行return或throw语句
对于“不可查异常”, 系统也会抛出它,写不写throws效果一样
即throws ArithmeticException不写, main方法仍能够正常捕捉到!会自动抛出public static int div(int m, int n) throws ArithmeticException {
执行的结果
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.ArithmeticException: / by zerom/n=0
转载地址:http://thyci.baihongyu.com/