java基础【异常的分类和三种处理异常的实例】

1、异常:程序在运行时有可能出现的非正常状况,一旦出现,会导致程序崩溃。

2、分类:

        (1)按照程度分:

                Error:虚拟机也无法处理的错误状况

                Exception:一般性问题

        (2)按照处理方式分:

                受检异常:在程序中必须对其进行处理的异常,如果不处理,编译就会出错

                        Exception及其子类,RuntimeException及其子类除外:问题程度不容忽视,比如IO异常。

                非受检异常:在程序中不一定要对其进行处理的异常

                        Error及其子类:太严重了

                        RuntimeException及其子类:太轻微了!

3、为了避免程序崩溃,我们应该对异常进行处理:

        (1)捕获:try catch finally

                try {

                        可能抛出异常的语句

                } catch (可能的异常类型 引用){

                        通过引用处理异常对象

                } catch (可能的异常类型2 引用){

                        通过引用处理异常对象

                } finally {        

                        //通常在这里释放不在GC区中的资源(USB,硬盘,网络等偏硬件的资源),为了保证不会发生资源泄露。

                }        

public static void main(String[] args) {
        System.out.println("main begin");

        try{
            int n = Integer.parseInt(args[0]);
            System.out.println(n);
        } catch (NumberFormatException e){
            System.out.println(e.getMessage()); //获取异常消息字符串
        } catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();    //打印栈踪迹
        } catch (Exception e){ //捕获其他任意异常
            System.out.println(e);
        } finally {
            System.out.println("无论前面发生了什么,都要执行finally ...");
        }

        System.out.println("main end");
    }

        (2)抛出异常:在签名中使用throws 可能的异常类型列表,警告调用者,使用此方法有风险,使用前请考虑清楚

                在方法中使用throw异常对象,方法一旦执行了throw和执行return效果是一样的,都会导致方法结束

                return是正常结束返回,throw是异常结束返回。

                方法中的throw是真的将异常抛出,真的产生破坏。

    public static int divide(int x, int y){
        if(y == 0){
            RuntimeException runtimeException = new RuntimeException("我是被零除异常");
            throw runtimeException;
        }
        return x / y;
    }

    public static void main(String[] args) {
        System.out.println("main begin");
        try{
            System.out.println(divide(10,2));
            System.out.println(divide(10,0));
        }catch (RuntimeException e){
            System.out.println(e);
        }
        System.out.println("main end");
    }

                       自定义异常,必须继承Exception,并提供两个构造器,是受检异常。

class DivideByZeroException extends Exception{

    public DivideByZeroException(Throwable cause) {
        super(cause);
    }

    //传递异常消息
    public DivideByZeroException(String message) {
        super(message);
    }
}

    public static int divide(int x,int y) throws DivideByZeroException { //警告使用者,这个方法可能会引起DivideByZero问题
        if(y == 0){
            throw new DivideByZeroException("自定义异常,被零除错误");//这是受检异常,是容易引起重大错误的异常,必须处理
        }
        return x / y;
    }

    public static void main(String[] args){
        System.out.println("main begin");
        try{
            System.out.println(divide(10,2));
            System.out.println(divide(10,0));
        }catch (DivideByZeroException e){
            System.out.println(e);
        }
        System.out.println("main end");
    }

        方法覆盖:子类重写父类的方法

        覆盖条件:

                返回值类型(子类方法返回值类型小于等于父类),方法名,参数列表要一致(签名)

                子类覆盖方法的访问控制修饰符要大于等于父类

                覆盖方法不可以被private,static,final修饰

                子类的覆盖方法不能抛出比父类方法抛出更大的受检异常(多态虚方法调用看父类,父类抛出范围小的异常,子类实际抛出的异常大于父类异常,不能够捕捉到父类异常)

        (3)先捕获再抛出:好处是把各种已知未知的异常全部包装为自定义异常,便于统一管理。

                 在方法中先常识执行某代码,如果真的出现了异常,再把这个异常关联到自定义异常对象中,再抛出自定义异常对象

    public static int divide(int x,int y) throws DivideByZeroException {
        try{
            return x / y;
        }catch (ArithmeticException e){
            throw new DivideByZeroException(e); //包装已知异常
        }
    }

    public static void main(String[] args) {
        System.out.println("main begin");

        try {
            System.out.println(divide(10,0));
        } catch (DivideByZeroException e) {
            e.printStackTrace();
        }
        System.out.println("main end");
    }

       总结:异常的作用相当于日志,当程序出现问题,用户不仅可以知道程序出现错误,还可以从日志中知道是什么原因造成的错误。

        异常处理的选择:入口方法捕获,普通方法抛出。

        补充:当方法出现问题会导致栈内所有方法不能执行,那么这个方法称为入口方法。

        如果代码中有潜在风险,尽量先捕获再抛出。

        如果代码中没有风险,但是有时不满足方法继续的条件时,直接抛出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneTenTwo76

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值