• 异常


    1 异常的继承体系

    Java代码在运行时期发生的问题就是异常。

    在Java中,把异常信息封装成了一个类。当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置、原因等)。

    Throwable: 它是所有错误与异常的超类(祖宗类)

             |- Error 错误

             |- Exception 编译期异常,进行编译JAVA程序时出现的问题

                 |- RuntimeException 运行期异常, JAVA程序运行过程中出现的问题

    2 抛出异常throw

    l  1,创建一个异常对象。封装一些提示信息(信息可以自己编写)。

    l  2,需要将这个异常对象告知给调用者。怎么告知呢?怎么将这个异常对象传递到调用者处呢?通过关键字throw就可以完成。throw 异常对象;

    使用格式:

    throw new 异常类名(参数);

    3 声明异常throws和捕获异常

    声明异常格式:

    修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2… {   }

     

     

    捕获异常格式:

    try {

        //需要被检测的语句。

    }

    catch(异常类 变量) { //参数。

        //异常的处理语句。

    }

    finally {

        //一定会被执行的语句。

    }

    public class Demo2 {
        //异常处理两种方式
        //throws Exception
        //try+catch+finally
        public static void main(String[] args) throws Exception{
            int[]arr={1,4,6,2,3,5};
            arr=null;
            try{
                //可能会发生异常的语句
                int i=get(arr);
                System.out.println(i);
            }catch(NullPointerException ex){
                //处理异常的方式
                System.out.println(ex);
            }catch(ArrayIndexOutOfBoundsException ex){
                System.out.println(ex);
            }
            finally{ 
                
            }
        }
        public static int get(int[]arr)throws NullPointerException,ArrayIndexOutOfBoundsException{
            if(arr==null){
                throw new NullPointerException("数组为空!");
            }
            if(arr.length<=6){
                //Exception 编译期异常+throws
                throw new ArrayIndexOutOfBoundsException("传递数组长度大于6的数组进来!");
            }
            
            int i=arr[5]+1;
            return i;
        }/*总结:异常分为编译时期异常和运行时期异常,运行时期异常,无需throws,只能修改代码
        编译时期异常,需要处理,需要加throws,或者try catch*/
    }

    4 异常的继承

    import java.text.ParseException;
    //异常在方法重写时的注意事项
    //如果父类的方法抛出异常,那么子类在 重写该方法时有两种选择
    //1.不抛
    //2.如果抛异常,只能抛父类方法异常的子类
    //如果父类方法不抛异常,那么子类重写该方法时不准抛出异常
    public class Fu {
        public void eat()throws Exception{
    }
        }
    class Zi extends Fu{
        public void eat()throws ParseException{
        }
    }
    public class FuShuException extends RuntimeException{
        public FuShuException(String s){
            super(s);
        }
    }
    public class Demo02 {
        public static void main(String[] args) {
            double avg=avg(1,2,3,4,5,-96);
            System.out.println(avg);
        }
        //下一个方法来求平均成绩
        public static double avg(double...score){
            double sum=0;
            for(double s:score){
                if(s<0){
                    throw new FuShuException("该"+s+"值为负数");
                }
                sum+=s;
            }
            return sum/score.length;
        }
    }
  • 相关阅读:
    函数与宏定义实验报告
    C语言作业3
    循环结构课后反思
    C语言作业2
    C程序设计实验报告
    百分制成绩五级分制输出--二次作业
    第九章 结构体与共用体
    第八章 指针实验
    第七章 数组实验
    第六章 函数和宏定义实验(2)
  • 原文地址:https://www.cnblogs.com/quanjunkang/p/10647253.html
Copyright © 2020-2023  润新知