异常

异常:程序执行时发生的不正常情况。

一旦出现异常,后边的语句没有机会执行,程序崩溃,提前结束。
因此我们在程序设计的时候要对异常进行处理。

异常分类:
按照严重程度:
1.Error严重错误
2.Exception普通异常
按照处理要求分类:
1.受检异常:在程序中必须接受检查和处理异常(Exception及其子类(除RuntimeException及其子类))
2.非受检异常:可以不接受检查和处理异常(Error及其子类:太严重)(RuntimeException及其子类:太轻微))

这里写图片描述

异常抛出:若是程序员不自己抛出,由JVM自动抛出,堆栈结构,异常对象抛给调用者。
这里写图片描述

异常处理:适用于所有异常
1.捕获
使用 try{有可能抛出异常的语句}
catch(捕获到的异常类型 异常对象){
异常处理
}
finally{这里的语句总是会执行,无论发生什么情况}可选
通常使用finally进行一些资源关闭的操作

try{}
finally{} 也行

捕获 使用语句try catch finally, 组合法3种, 至少保证要有try,还必须有catch或finally
程序练习:

package com.atguigu.javase.exception;
/**
 *异常测试:
 *1.若某个参数不是数字,Exception in thread "main" java.lang.NumberFormatException: For input string: "z"
 *2.什么都不传,Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
 *3.第二个参数为0,Exception in thread "main" java.lang.ArithmeticException: / by zero
 *
 *1.捕获异常    
 */
public class ExceptionTest {
    public static void main(String[] args) {
        System.out.println("main begin...");
        try {
            int i1 = Integer.parseInt(args[0]);
            int i2 = Integer.parseInt(args[1]);
            System.out.println(i1 / i2);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            System.out.println(e);
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println("其他异常" + e);
        } finally {
            System.out.println("这个打印语句总是会执行,无论什么情况");
        }

        System.out.println("main end...关键代码"); //程序关键代码
    }
}

创建自定义异常:
a.自定义异常继承Exception
构造器: public 类名(message) {super(message)}
public 类名(cause) {super(cause)}
b.在语句块中使用throw做出抛出动作,并在方法签名中使用throws声明
c.在main方法中对异常做出处理(建议:不能再抛)

2.抛出 在方法签名中使用throws 可能抛出的异常类型, 在语句块中使用throw 异常对象 真的抛出异常, 产生实质破坏

throws 后面可以跟多个不同类型的异常,表示此方法可能抛出多个异常。
程序练习:

package com.atguigu.javase.exception;
/**
 * 2.抛出自定义异常
 * @author LENOVO
 *
 */
public class ExceptionTest2 {
    //throws后边跟的是抛出异常的声明
    public static int divide(int m , int n) throws IlleagalNumberException {
        if(n == 0) {
            //当n==0是抛出IlleagalNumberException异常
            throw new IlleagalNumberException("输出非法数字0");
        }
        return m / n;
    }

    public static void main(String[] args) {
        //main中处理异常
        System.out.println("main begin...");
        try {
            int m = Integer.parseInt(args[0]);
            int n = Integer.parseInt(args[1]);
            int result = divide(m, n);
            System.out.println(m + "/" + n + "=" + result );
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            System.out.println(e);
        } catch (IlleagalNumberException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println("其他异常" + e);
        }
        System.out.println("main end...关键代码");
    }
}

3.捕获并抛出异常
先捕获到已知异常,捕获到以后,再把它包装成其他自定义的异常对象,再抛出自定义异常对象。

package com.atguigu.javase.exception;
/**
 * 3.捕获并包装异常
 * @author LENOVO
 *
 */
public class ExceptionTest3 {

    public static int divide (int m, int n) throws IlleagalNumberException {
        try {
            return m / n;
        } catch (ArithmeticException e) {
            throw new IlleagalNumberException(e);
        }
    }

    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 (IlleagalNumberException e) {
            System.out.println(e);
        }
        System.out.println("main end...关键代码");
    }
}

处理方式的选择:
底层方法:抛出异常或者捕获在抛出
顶层方法:捕获处理

方法覆盖 : 子类重写父类的方法.
条件 :
1) 方法签名必须一致 (返回值类型一致, 方法名一致, 参数列表一致[参数类型一致, 个数一致, 顺序一致]
2) 访问控制修饰符子类大于等于父类
3) 子类抛出的受检异常范围小于等于父类

最后:对异常进行处理后程序将异常消化掉,后续的关键代码可以得到执行
使用System.out.println(e); // 相对详细
e.printStackTrace(); // 异常的信息最详细的
System.out.println(e.getMessage()); // 最不详细

加油!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值