zoukankan      html  css  js  c++  java
  • Java异常处理-----非运行时异常(受检异常)

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

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

    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:子类不能比父类抛出更多的异常


    【正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个“顶”字,你就顺手把它点了吧(要先登录CSDN账号哦 )】


    —–乐于分享,共同进步!
    —–更多文章请看:http://blog.csdn.net/duruiqi_fx


  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/hainange/p/6153839.html
Copyright © 2011-2022 走看看