4异常处理
异常处理
1 异常概述和异常体系结构
 异常概述
- java程序在执行过程中所发生的异常事件可以分为两类Error和Exception
- Error: java虚拟机无法解决的严重问题。比如JVM系统内部错误,资源耗尽等严重情况。比如StackOverflowError和OOM。一般不编写针对性的代码进行处理
- Exception: 其他因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理,比如空指针访问,试图读取不存在的文件,网络连接中断,数据角标越界
 异常体系结构
/*
异常体系结构
 java.lang.Throwable
     java.lang.Error:一般不编写针对性代码进行处理
     java.lang.Exception:可以进行异常的处理
         编译时异常(checked)(又叫受检异常)
             IOException
                 FileNotFoundException
             ClassNotFoundException
         运行时异常(unchecked)(又叫非受检异常)
             NullPointerException
             ArrayIndexOutOfBoundsException
             ClassCastException
             NumberFormatException
             InputMismatchException
             ArithmeticException
*/
public class Fao84ErrorTest {
    //NullPointerException
    @Test
    public void test1(){
        int[] arr=null;
        System.out.println(arr[3]);
    }
    //ArrayIndexOutOfBoundsException
    @Test
    public void test2(){
//        int[] arr=new int[3];
//        System.out.println(arr[3]);
//        StringIndexOutOfBoundsException
        String str="abc";
        System.out.println(str.charAt(3));
    }
    // ClassCastException
    @Test
    public void test3(){
        Object obj=new Date();
        String str=(String) obj;
    }
    // NumberFormatException
    @Test
    public void test4(){
        String str="abc";
        int num=Integer.parseInt(str);
    }
    // InputMismatchException
    @Test
    public void test5(){
        Scanner scanner=new Scanner(System.in);
        int score=scanner.nextInt();
        System.out.println(score);
    }
    //ArithmeticException
    @Test
    public void test6(){
        int a=10;
        int b=0;
        System.out.println(a/b);
    }
    //*********以下是编译时异常
    //IOException
    @Test
    public void test7() throws IOException {
        File file =new File("hello.txt");//找不到指定文件
        FileInputStream fis=new FileInputStream(file);
        int data=fis.read();
        while(data!=-1){
            System.out.println((char)data);
            data=fis.read();
        }
        fis.close();
    }
}
2 异常处理:抓抛模型和两种处理方式
 2.1 抓抛模型
 过程一:抛
- 程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个异常类的对象,并将此对象抛出
- 一旦抛出对象以后 其后的代码就不再执行
- 关于异常对象的产生:1系统自动生成的异常对象:程序运行过程中,虚拟机检测到程序发生了问题如果在当前代码中没有找到相应的处理程序,就会在后台自动创建一个对应异常的实例对象并抛出 2手动生成一个异常对象 并抛出throw:Exception exception=new ClassCastException();创建好的异常对象不抛出对程序没有任何影响,和创建一个普通对象一样
 过程二:抓
- 可以理解为异常处理的方式:1try-catch-finally 2throws
 2.2 处理方式一:try-catch-finally
 结构
    try{
        //可能出现异常的代码
    }catch(异常类型1 变量名1){
        //处理异常的方式1
    }catch(异常类型2 变量名2){
        //处理异常的方式2
    }
    ...
    finally{
        //一定会执行的代码
    }
 说明:
- 使用try将可能出现异常代码包装起来 在执行过程中 一旦出现异常 就会生成一个异常类的对象 根据此对象的类型 去catch中进行匹配
- 一旦try中异常对象匹配到某一个catch时,就进入catch中进行异常处理
- 一旦处理完成就跳出当前try-catch结构(没有finally情况下)继续执行后面的代码
- catch中的异常类型如果没有子父类关系 则没有声明顺序关系, 但如果有子父类关系 则如果父亲在上面 会报错
- 常用异常对象处理方式 1String getMessage() 2printStackTrace()
- 在try结构中声明的变量 再出了try结构以后就不能再使用 可以声明在外面 里面使用 开发中经常在外面先声明为null或0 这样里面就能直接使用
- 使用try-catch-finally处理编译时异常,使得程序在编译时不再报错 但运行时还会报错,相当于我们将一个编译可能出现的异常延迟到运行时再报错
- try-catch结构可以相互嵌套
- finally是可选的
- finally声明的是一定会被执行的代码 即使catch中又出现了异常或者 try/catch中有return语句
- 像数据库连接 输入输出流网络编程socket JVM不能自动释放 需要手动释放 需要放在finally中
- 开发中由于运行时异常比较常见 所以我们通常就不针对运行异常编写try-catch
- 针对编译时异常 一定要考虑异常的处理
public class Fao85ErrorTest2 {
    @Test
    public void test1(){
        String str="abc";
        try {
            int num=Integer.parseInt(str);
            System.out.println(1);//没执行
        }catch (NumberFormatException e){
			//System.out.println("出现数值转换异常了,不要着急");
            //e.getMessage()
            System.out.println(e.getMessage());//For input string: "abc"
            //e.printStackTrace()
        }catch (Exception e){
            System.out.println("出现异常了,不要着急");//没执行
        }
        System.out.println(2);//2
    }
    @Test
    public void test2(){
        try {
            int a=10;
            int b=0;
            System.out.println(a/b);
        }catch (ArithmeticException e){
            e.printStackTrace();//方式二
            return;
        }catch (Exception e){
            e.printStackTrace();
        }
        finally{
            System.out.println("啦啦");//啦啦 
        }
    }
}
 2.3 处理方式二:throws+异常类型
 使用
- throws+异常类型 写在方法的声明处 指明此方法执行时 可能会抛出的异常类型
- 一旦方法体执行后 出现异常 仍会在异常代码处生成一个异常类的对象,此对象满足throws异常类型时 就会被抛出
- 异常代码后续的代码就不再执行
- try-catch-finally真正将异常给处理掉了;throws只是将异常抛给了方法的调用者 并没有真正将异常处理掉
- 注意:子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型
 开发中两种方法如何选择
- 如果父类中被重写的方法没有throws方式处理异常 则子类重写的方法也不能使用throws,意味着如果子类重写方法中有异常 必须使用try-catch方式来处理
- 执行方法中先后又调用了另外几个方法, 这几个方法是递进关系 建议这几个方法用throws,最后再统一执行try-catch
public class Fao86ErrorTest3 {
    //在main中
    public static void main(String[] args) {
        try {
            method2();
        }catch (IOException e){
            e.printStackTrace();
        }
       method3();
    }
    //在静态函数中
    public static void method3(){
        try {
            method2();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void method2() throws IOException{
        method1();
    }
}    
2.4 自定义异常类
 注意throw和throws区别
 throw:用于手动抛出异常对象,异常生成阶段
 throws:声明方法可能要抛出的克制异常,异常的处理方式
- 继承于现有的异常结构:RuntimeException Exception
- 提供 全局常量serialVersionUID
- 提供重载的构造器
public class Fao89ErrorTest6 extends RuntimeException {//1
    static final long serialVersionUID=-70348971999999L;//2
    public Fao89ErrorTest6(){//3
    }
    public Fao89ErrorTest6(String msg){//3
        super(msg);
    }
}
public class Fao88ErrorTest5 {
    public static void main(String[] args) {
        Student s=new Student();
        s.regist(1001);
        System.out.println(s);
        s.regist(-1);
    }
}
class Student{
    int id;
    public void regist(int id){
        if(id>0){
            this.id=id;
        }else {
			//System.out.println("违法");
            //可以手动抛出一个异常
			//throw new RuntimeException("违法");
            throw new Fao89ErrorTest6("自己的exception");//抛出自己定义的异常
        }
    }
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号