47.java异常处理之运行时异常和非运行时异常

 运行时异常和非运行时异常

1.   RuntimeException

RunntimeException的子类: 

             ClassCastException

                多态中,可以使用Instanceof 判断,进行规避

             ArithmeticException

                进行if判断,如果除数为0,进行return

             NullPointerException

                进行if判断,是否为null

             ArrayIndexOutOfBoundsException

                使用数组length属性,避免越界

             这些异常时可以通过程序员的良好编程习惯进行避免的

1:遇到运行时异常无需进行处理,直接找到出现问题的代码,进行规避。

                2:就像人上火一样牙疼一样,找到原因,自行解决即可

                3:该种异常编译器不会检查程序员是否处理该异常

                4:如果是运行时异常,那么没有必要在函数上进行声明。

             6:案例

                1:除法运算功能(div(int x,int y))

                2:if判断如果除数为0,throw new ArithmeticException();

                3:函数声明throws ArithmeticException

                4:main方法调用div,不进行处理

                5:编译通过,运行正常

                6:如果除数为0,报异常,程序停止。

                7:如果是运行时异常,那么没有必要在函数上进行声明。

1:Object类中的wait()方法,内部throw了2个异常 IllegalMonitorStateException InterruptedException

1:只声明了一个(throws) IllegalMonitorStateException是运行是异常没有声明。

class Demo12 {

    public static void main(String[] args){
        div(2, 1);
    }

    public static void div(int x, int y) {
        if (y == 0) {
            throw new ArithmeticException();  
        }
        System.out.println(x / y);
    }
}

  

2.   非运行时异常(受检异常)

如果出现了非运行时异常必须进行处理throw或者try{}catch(){}处理,否则编译器报错。

          1;IOException  使用要导入包import java.io.IOException;

                2:ClassNotFoundException

             2;例如人食物中毒,必须进行处理,要去医院进行处理。

             3:案例

                1:定义一测试方法抛出并声明ClassNotFoundException(test())

                2:main方法调用test

                3:编译报错

                   1:未报告的异常 java.lang.ClassNotFoundException;必须对其进行捕捉或声明以便抛出

public void isFile(String path){
        try
        {
            /*
            根据文件的路径生成一个文件对象,如果根据该路径找不到相应的文件,
            则没法生成文件对象。
            */
            File file = new File(path);
            //读取文件的输入流
            FileInputStream input = new FileInputStream(file);
            //读取文件
            input.read();
        }
        catch (NullPointerException e)
        {
            System.out.println("读取默认的文件路径..");
        }
        
    }

    4:Sun 的API文档中的函数上声明异常,那么该异常是非运行是异常,调用者必须处理。

              5:自定义异常一般情况下声明为非运行时异常

      2:函数的重写和异常

          1:运行时异常

             1:案例定义Father类,定义功能抛出运行是异常,例如(test() throw ClassCastException)

             2:定义Son类,继承Father类,定义test方法,没有声明异常

             3:使用多态创建子类对象,调用test方法

             4:执行子类方法

      1:函数发生了重写,因为是运行时异常,在父类的test方法中,可以声明 throws 也可以不声明 throws  

class Father {
    void test() throws ClassCastException { // 运行时异常
        System.out.println("父类");
        throw new ClassCastException();
    }
}

class Son extends Father {
    void test() {
        System.out.println("子类");
    }
}
class Demo14 {

    public static void main(String[] args) {
        Father f = new Son();
        f.test();
    }
}

      2:非运行时异常

        1:定义父类的test2方法,抛出非运行时异常,例如抛出ClassNotFoundException

                   1:此时父类test2方法必须声明异常,因为是非运行时异常

                   2:Son类定义test2 方法,抛出和父类一样的异常,声明异常

                   3:使用多态创建子类对象,调用test方法,调用test2方法,

            1:声明非运行时异常的方法,在调用时需要处理,所以在main方法调用时throws

                       2:实现了重写,执行子类的test2方法

                    3:总结子类重写父类方法可以抛出和父类一样的异常,或者不抛出异常。

//  1 子类覆盖父类方法父类方法抛出异常,子类的覆盖方法可以不抛出异常
class Father {
    void test() throws ClassNotFoundException { // 非运行时异常
        System.out.println("父类");
        throw new ClassNotFoundException();
    }
}

class Son extends Father {
    void test() {
        System.out.println("子类");
        // 父类方法有异常,子类没有。
    }
}
class Demo14 {

    public static void main(String[] args) throws ClassNotFoundException  {
        Father f = new Son();
        f.test();

    }
}

          4:子类抛出并声明比父类大的异常例如子类test2方法抛出Exception

                        1:编译失败,无法覆盖

                        2:子类不能抛出父类异常的父类。

                        3:总结子类不能抛出比父类的异常更大的异常。

//2:子类覆盖父类方法不能比父类抛出更大异常
class Father {
    void test() throws Exception {
        // 非运行时异常
        System.out.println("父类");
        throw new Exception();
    }
}

class Son extends Father {
    void test() throws ClassNotFoundException { // 非运行时异常
        System.out.println("子类");
        throw new ClassNotFoundException();
    }
}
class Demo14 {

    public static void main(String[] args) throws Exception {
        Father f = new Son();
        f.test();

    }
}

    3:总结

               1:子类覆盖父类方法是,父类方法抛出异常,子类的覆盖方法可以不抛出异常,或者抛出父类方法的异常,或者该父类方法异常的子类。

               2:父类方法抛出了多个异常,子类覆盖方法时,只能抛出父类异常的子集

               3:父类没有抛出异常子类不可抛出异常

                  1:子类发生非运行时异常,需要进行try{}catch的(){}处理,不能抛出。

             4:子类不能比父类抛出更多的异常

author@nohert
原文地址:https://www.cnblogs.com/gzgBlog/p/13588947.html