摘要:韦东山android视频学习笔记
java的异常处理的原则如下:
1、我们先写一个没有对异常处理的程序,在进行除法运算的时候,除数是非零的话,运行时没有问题的,但是除数为零的时候,运行就会有问题,程序也不能往下执行(只打印了Begin of div)
1 public class Div{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 7 System.out.println("Begin of div"); 8 int r = div(m,n); 9 System.out.println("end of div"); 10 11 System.out.println(m+"/"+n+"="+r); 12 } 13 14 public static int div(int m,int n){ 15 int r = m / n; 16 return r; 17 } 18 }
编译运行:
2、我们先写一个有对异常进行处理程序(自己处理异常),根据下面的运行结果,程序可以捕获到异常并且可以正常的执行.
1 public class Div2{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 7 System.out.println("Begin of div"); 8 int r = div(m,n); 9 System.out.println("end of div"); 10 11 System.out.println(m+"/"+n+"="+r); 12 } 13 14 public static int div(int m,int n){ 15 int r = 0; 16 try { 17 r = m / n ; 18 }catch (ArithmeticException e){ 19 System.out.println(e); 20 }finally{ 21 System.out.println("This is finally of div"); 22 } 23 24 return r; 25 } 26 }
编译运行:
3、我们写一个程序将异常抛出的类,这个抛出的异常是由main进行处理.
1 public class Div4{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 r = div(m,n); 11 }catch (ArithmeticException e){ 12 System.out.println(e); 13 } 14 System.out.println("end of div"); 15 16 System.out.println(m+"/"+n+"="+r); 17 } 18 19 public static int div(int m,int n) throws ArithmeticException{ 20 int r = 0; 21 22 r = m / n ; 23 24 return r; 25 } 26 }
编译运行:
4、如果在类的方法中如果处理了异常,那样在main方法中就不会对异常进行处理.
1 public class Div5{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 r = div(m,n); 11 }catch (ArithmeticException e){ 12 System.out.println(e); 13 } 14 System.out.println("end of div"); 15 16 System.out.println(m+"/"+n+"="+r); 17 } 18 19 public static int div(int m,int n) throws ArithmeticException{ 20 int r = 0; 21 22 try{ 23 r = m / n ; 24 }catch(ArithmeticException e){ 25 System.out.println("div :"+e); 26 } 27 28 return r; 29 } 30 }
编译运行:
5、如果在类的方法中如果处理了异常,同时在类方法中把异常抛出,那样main方法也可以捕获到异常.
1 public class Div6{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 r = div(m,n); 11 }catch (ArithmeticException e){ 12 System.out.println(e); 13 } 14 System.out.println("end of div"); 15 16 System.out.println(m+"/"+n+"="+r); 17 } 18 19 public static int div(int m,int n) throws ArithmeticException{ 20 int r = 0; 21 22 try{ 23 r = m / n ; 24 }catch(ArithmeticException e){ 25 System.out.println("div :"+e); 26 throw e; 27 } 28 29 return r; 30 } 31 }
编译运行:
6、现在我们上面第5个例子的代码,只有对这种算术运行的异常进行处理,如果我传入的参数个数不对,还有参数的格式也不对,程序是处理不了的。
为了修复上述的问题,我们添加对传入参数格式不对,还有传入参数个数不对这两种异常的处理。
1 public class Div7{ 2 3 public static void main(String args[]){ 4 int m = 0; 5 int n = 0; 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 m = Integer.parseInt(args[0]); 11 n = Integer.parseInt(args[1]); 12 r = div(m,n); 13 }catch (ArithmeticException e){ 14 System.out.println("main :" + e); 15 }catch (NumberFormatException e){ 16 System.out.println("main :" + e); 17 }catch (ArrayIndexOutOfBoundsException e){ 18 System.out.println("main :" + e); 19 } 20 System.out.println("end of div"); 21 22 System.out.println(m+"/"+n+"="+r); 23 } 24 25 public static int div(int m,int n) throws ArithmeticException{ 26 int r = 0; 27 28 try{ 29 r = m / n ; 30 }catch(ArithmeticException e){ 31 System.out.println("div :"+e); 32 throw e; 33 } 34 35 return r; 36 } 37 }
编译运行结果
7、在第6个例子继续优化,上面的程序目前只能对算术运算、参数格式还有参数个数不对的异常进行处理,其他的情况是无法处理的到的,我们可以添加对这些异常的父类RuntimeException来捕获异常.
1 public class Div8{ 2 3 public static void main(String args[]){ 4 int m = 0; 5 int n = 0; 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 m = Integer.parseInt(args[0]); 11 n = Integer.parseInt(args[1]); 12 r = div(m,n); 13 }catch (ArithmeticException e){ 14 System.out.println("main :" + e); 15 }catch (NumberFormatException e){ //去掉了参数个数的异常,仍然可以捕获到 16 System.out.println("main :" + e); 17 }catch (RuntimeException e){ 18 System.out.println("main :" + e); 19 } 20 System.out.println("end of div"); 21 22 System.out.println(m+"/"+n+"="+r); 23 } 24 25 public static int div(int m,int n) throws ArithmeticException{ 26 int r = 0; 27 28 try{ 29 r = m / n ; 30 }catch(ArithmeticException e){ 31 System.out.println("div :"+e); 32 throw e; 33 } 34 35 return r; 36 } 37 }
编译运行结果
8、对于“不可查异常”, 系统也会抛出它,写不写throws效果一样
1 public class Div9{ 2 3 public static void main(String args[]){ 4 int m = 0; 5 int n = 0; 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 m = Integer.parseInt(args[0]); 11 n = Integer.parseInt(args[1]); 12 r = div(m,n); 13 }catch (ArithmeticException e){ 14 System.out.println("main :" + e); 15 }catch (NumberFormatException e){ 16 System.out.println("main :" + e); 17 }catch (RuntimeException e){ 18 System.out.println("main :" + e); 19 } 20 System.out.println("end of div"); 21 22 System.out.println(m+"/"+n+"="+r); 23 } 24 25 //public static int div(int m,int n) throws ArithmeticException{ 26 public static int div(int m,int n){ 27 int r = 0; 28 29 try{ 30 r = m / n ; 31 }catch(ArithmeticException e){ 32 System.out.println("div :"+e); 33 throw e; 34 }finally{ 35 System.out.println("finally of div"); 36 } 37 38 return r; 39 } 40 }
编译运行:
相关代码存放在github,可以下载https://github.com/zzb2760715357/100ask