java面向对象学习--exception

异常:就是程序在运行时出现不正常情况.
异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象,其实就是java对不正常情况进行描述后的对象的体现
对于问题的划分:
一种是严重的问题,java通过Error类进行描述
—-对于error一般不编写针对性的代码对其进行处理
对于非严重的,java通过Exception类进行描述
—–对于exception可以使用针对性的处理方式进行处理
无论Error还是Exception都具有一些共性内容,比如:不正常情况的信息,引发原因等.

所有的错误和异常都继承自Throwable父类
Throwable
__|–Error
__|–Exception

异常的处理
java提供了特有的语句进行处理
try{
需要被检测的代码
}catch(异常类 变量){
处理异常的代码:(处理方式)
}finally{
一定会执行的代码
}
对捕获到的异常对象进行常见方法操作:e.getMessage(),e.toString(),e.printStackTrace();
注:java虚拟机默认的异常处理机制就是调用printStackTrace()方法打印异常的堆栈跟踪信息
示例

public class ExceptionDemo {

    public static void main(String[] args) {
        try {
            int x = 3/0;
            System.out.println(x);
        }catch(Exception e) {
            System.out.println("除数为零,无意义....");
            System.out.println("异常信息:"+e.getMessage());//异常信息
            System.out.println(e.toString());//异常类名 :异常信息
            e.printStackTrace();//异常信息+异常位置
        }
        System.out.println("程序执行完毕...");
    }
}

结果

除数为零,无意义....
异常信息:/ by zero
java.lang.ArithmeticException: / by zero
程序执行完毕...
java.lang.ArithmeticException: / by zero
    at com.yangqi.practice.ExceptionDemo.main(ExceptionDemo.java:7)

printStackTrace()函数打印的信息是在主函数的最后.

用关键字throws在函数上声明异常,便于提高安全性,让调用处进行处理.不处理编译失败


public class ExceptionDemo {
    public static void main(String[] args) {
        demo d = new demo();
        try {
            int i = d.div(4, 0);
            System.out.println(i);
        } catch (Exception e) {
            System.out.println(e.toString());
            //e.printStackTrace();
        }
    }
    static class demo{
        public int div(int a,int b)throws Exception {
            return a/b;
        }
    }

多异常的处理
1.声明异常时,建议声明更为具体的异常.这样处理的可以更具体
如:上述的Exception可以具体为ArithmeticException异常
2.throws在抛出异常时可以抛出多个异常类,用’,’隔开,声明几个异常,就写几个catch处理异常
如果多个catch块中的异常出现继承关系,父类异常catch块放在下面,否则父类异常就会处理自类异常.

示例

public class ExceptionDemo {
    public static void main(String[] args) {
        demo d = new demo();
        try {
            int i = d.div(4, 0);
            System.out.println(i);
        } catch (ArithmeticException e) {
            System.out.println("除数为零异常....");
        }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常....");
        }
        System.out.println("over...");
    }
    static class demo{
        public int div(int a,int b)throws ArithmeticException,
        ArrayIndexOutOfBoundsException 
        {
            int[] x = new int[a];
            System.out.println(x[5]);
            return a/b;
        }
    }
}

运行结果

数组越界异常....
over...

这个程序有两个异常出现,但是只打印了一个,说明函数遇到第一个ArrayIndexOutOfBoundsException异常时就会停止执行,try找到对应的catch处理异常.

注:写一个catch也可以,用异常的父类接收异常就可以..catch(Exception e){}

自定义异常
因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象.
所以对于这些特有的问题可以按照java的对问题封装的思想.
将特有的问题,进行自定义的异常封装

步骤
1.自定义异常类,继承Exception
2.在问题出通过throw手动抛出自定义异常
当函数内部出现了throw抛出异常,那么就必须要给对应的处理动作
要么在内部try catch处理
要么在函数上声明让调用者处理
一般情况,在函数内出现异常,函数上需要声明
示例

public class ExceptionDemo2 {

    public static void main(String[] args) {

        //主函数中处理异常
        try {
            int x = div(3,-2);
            System.out.println(x);
        }catch(FuShuException e) {
            System.out.println("e.toString");
            System.out.println("除数为负数异常....");
        } 
        System.out.println("over");
    }

    //函数中除数为负数时抛出自定义异常
    public static int div(int a, int b) throws FuShuException 
    ,ArithmeticException{
        //判断除数是否小于零
        if(b<0)
            //抛出异常
            throw new FuShuException();
        return a/b;

    }

}
//自定义异常类
public class FuShuException extends Exception {

}

运行结果

com.yangqi.exception.FuShuException
除数为负数异常....
over

发现打印结果中只有异常的名称,却没有异常信息,这是因为自定义的异常并未定义异常信息

如何定义异常信息?
1.Exception中打印错误信息的方法是getMessage(),在编写自定义异常时重写该方法即可
2.在父类Throwable中提供一个Throwable(String msg)的构造方法.所以也可以不用重写,直接在构造方法中调用就可以super(String msg)就ok,也可以自己写方法反回具体的异常值.同样直接调用printStackTrace()可以打印出异常信息和异常位置
示例

public class ExceptionDemo2 {

    public static void main(String[] args) {

        //主函数中处理异常
        try {
            int x = div(3,-2);
            System.out.println(x);
        }catch(FuShuException e) {
            e.printStackTrace();
            //System.out.println(e.toString());
            System.out.println(e.getValue());
            //System.out.println("除数为负数异常....");
        }
        System.out.println("over");
    }

    //函数中除数为负数时抛出自定义异常
    public static int div(int a, int b) throws FuShuException ,ArithmeticException{
        //判断除数是否小于零
        if(b<0)
            //抛出异常
            throw new FuShuException("除数为负数异常....",b);
        return a/b;

    }

}

//自定义异常类
public class FuShuException extends Exception {

    private int value;
    //无参构造方法
    public FuShuException() {
        super();
    }
    //自定义异常信息
    FuShuException(String msg,int value) {
        super(msg);
        this.value=value;
    }
    //获取导致异常的数据
    public int getValue() {
        return value;
    }
}

运行结果

com.yangqi.exception.FuShuException: 除数为负数异常....
-2
    at com.yangqi.exception.ExceptionDemo2.div(ExceptionDemo2.java:24)
    at com.yangqi.exception.ExceptionDemo2.main(ExceptionDemo2.java:9)
over

继承Exception的原因
异常体系有一个特点:因为异常类和异常对象都被抛出
他们都具有可抛性,这个可抛性是Throwable这个体系中独有特点
只有这个体系中的类和对象才可以被throw和throws操作
throw和throws
throws使用在函数上
throw使用在函数内
throws后面跟的是异常类,可以有多个,用逗号隔开
throw后跟的是异常对象

RunTimeException
Exception中有一个特殊的自类异常RunTimeException运行时异常.
如果在函数内抛出该异常,函数上可以不用声明,编译一样通过
如果在函数上声明了该异常,调用者可以不用处理,编译一样通过.
之所以不用在函数上声明,是因为不需要让调用者处理
当该异常发生,希望程序停止,因为在运行时出现了无法继续运算的情况,希望停止程序后,对代码进行修正

自定义异常时:如果该异常的发生,无法再继续进行运算,就让自定义异常继承RunTimeException

猜你喜欢

转载自blog.csdn.net/fly_fly_fly_pig/article/details/81140124