用户名同QQ号

 

java之异常

 1 package com.oracle.demo01;
 2 //Throwable 的子类错误  Error:一旦发生,不可处理,只能修改代码,返回给jvm,结束程序的运行
 3 //Throwable 的子类异常 Expection
 4 //1.Expection编译期异常:可以处理
 5 //2.runtimeExpection运行期异常,不可处理,相当于Error
 6 public class Demo01 {
 7     public static void main(String[] args) {
 8         //  int[] arr=new int[10000000000];
 9     }
10   }

将异常交给调用者处理  try-----catch----finally

 1 package com.oracle.demo01;
 2                                                 //终止程序,打印异常信息
 3 public class Demo02 {
 4 public static void main(String[] args){//4.抛给虚拟机
 5     int[] arr={};
 6     try{//可能会发生异常的代码
 7         int a=get(arr);//3.调用者知道错误(最好交由调用者处理异常)try---catch{接受throws异常}---finally{释放资源代码,一定会执行}
 8         System.out.println(a);
 9     }catch(Exception ex){//异常类  变量名    3.捕获到异常
10         //一旦发生异常,才执行
11         ex.printStackTrace();//调用jvm虚拟机中的打印方法
12         //System.out.println("出现数组越界异常");
13     }finally{
14         //不管是否有异常,均会执行
15         System.out.println("一定会执行的代码");
16     }
17     System.out.println("正常执行余下的程序");
18 }
19 public static int get(int[] arr) throws Exception{//2.throws进行声明,让调用者去处理
20     if(arr.length==0){
21         //编译期异常(调用者处理,或虚拟机处理)
22         throw new Exception();//1.抛出异常,但还没有被捕获处理
23     }
24     int i=arr[3];//jvm检测到异常,创建对应的异常对象, new Arrayindexoutofboundsexpection();但没有能处理它的方式
25                  //往上抛给调用者,main方法中get(),又没有处理异常的方式,再往上抛给虚拟机
26                  //终止程序,不再往下运行
27                  //以红字的形式,将异常对象,异常信息,异常位置打印到控制台。
28     return i;
29 }
30 }

父类异常放在子类异常的下边处理

 1 package com.oracle.demo01;
 2                                                 //终止程序,打印异常信息
 3 public class Demo03 {
 4 public static void main(String[] args) throws Exception {//4.抛给虚拟机
 5     int[] arr={1,2,3};
 6     try{//可能会发生异常的代码
 7         int a=get(arr);//3.调用者知道错误(最好交由调用者处理异常)try---catch{接受throws异常}---finally{释放资源代码,一定会执行}
 8         System.out.println(a);
 9     }catch(ArrayIndexOutOfBoundsException ex){//子类异常放上面
10         
11     }catch(NullPointerException ex){//子类异常放上面
12         
13     }//父类异常放最下边
14     catch(Exception ex){//异常类  变量名    3.捕获到异常
15         //一旦发生异常,才执行
16         ex.printStackTrace();//调用jvm虚拟机中的打印方法
17         //System.out.println("出现数组越界异常");
18     }finally{
19         //不管是否有异常,均会执行
20         System.out.println("一定会执行的代码");
21     }
22     System.out.println("正常执行余下的程序");
23 }
24 public static int get(int[] arr) throws Exception,NullPointerException,
25 ArrayIndexOutOfBoundsException{//2.throws进行声明,让调用者去处理
26     if(arr.length==0){
27         //编译期异常(调用者处理,或虚拟机处理)
28         throw new ArrayIndexOutOfBoundsException();//1.抛出异常,但还没有被捕获处理
29     }
30     if(arr==null){
31         throw new NullPointerException();
32     }
33     if(arr.length<3){//这个方法针对下面取arr[3]是否能成功
34         throw new Exception();
35     }
36     int i=arr[3];//jvm检测到异常,创建对应的异常对象, new Arrayindexoutofboundsexpection();但没有能处理它的方式
37                  //往上抛给调用者,main方法中get(),又没有处理异常的方式,再往上抛给虚拟机
38                  //终止程序,不再往下运行
39                  //以红字的形式,将异常对象,异常信息,异常位置打印到控制台。
40     return i;
41 }
42 }

posted on 2020-01-06 10:30  用户名同QQ号  阅读(185)  评论(0编辑  收藏  举报

导航