Java的异常处理

Java的异常处理

一、什么是异常

程序在执行过程中发生的某些意外,例如:类型错误、内存溢出、文件丢失等,英文名为:Exception

二、异常体系结构

  1. Java可以把异常当作一个对象来处理,并定义了一个基类 java.lang.Throwable 作为所有异常的超类。
  2. 在Java API中已经定义了许多异常类,这些异常类分为两大类:错误:Error和异常:Exception

三、Java异常处理机制

  1. 关键字:try、catch、finally、throw、throws
  2. 语法
//若捕获多个异常,需要从小到大进行捕获
try{
    //需要被监视的代码块
}catch(Error e){
    //捕获错误
    //注意:错误不是异常,错误是程序员不可预见的,一般由JVM抛出
}catch(Exception e){
    //捕获异常
    //Exception与Error都是Throwable的子类,是平级关系,所以类似if语句,按照程序抛出的类型二者只捕获其一
}catch(Throwable e){
    //Throwable是Java提供的超类,可捕获任意异常或者错误,相当于对象中的object
}finally{
    //善后工作
}
  1. 快捷键(IDEA)

    选中需要被监视的代码块,按住CTRL+ALT+T,自动生成异常捕获处理。

  2. 代码示例

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try{ //监控区域
            int x = a/b;
        }catch (ArithmeticException e){ //参数为想要捕获的异常类型
           //捕获异常
            System.out.println("出现异常");
            //打印异常信息
            e.printStackTrace();
        }finally {//无论是否存在异常,都执行其中语句,用于善后工作,例如:关闭Scanner。可以省略。
            System.out.println("处理善后工作");
        }
    }
}
  1. throw 与 throws的区别

    throw 用于方法内部抛出,方法内部自己进行处理
    throws 用于方法上抛出,交由上一级处理
    例:

    ①throw抛出异常

import java.lang.Exception;

public class Test{
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        //throw 抛出异常 division_1
        int x = division_1(a,b);
    }
    static int division_1(int a,int b){
        try{ //在方法内部进行监听捕获和处理
            if(b==0){
                throw new ArithmeticException();
            }
        }catch(ArithmeticException e){ 
            System.out.println(e.toString());
        }
        return a/b;
    }
}

​ ②throws抛出异常

import java.lang.Exception;

public class Test{
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        //throws 抛出异常
        try{ //监控区域
            int y = division_2(a,b);
        }catch (ArithmeticException e){ //参数为想要捕获的异常类型
            //捕获异常
            System.out.println("出现异常");
            //打印异常信息
            e.printStackTrace();
        }finally {//无论是否存在异常,都执行其中语句,用于善后工作,例如:关闭Scanner。可以省略。
            System.out.println("处理善后工作");
        }
    }
    static int division_2(int a,int b) throws ArithmeticException{
        if(b==0){
            throw new ArithmeticException(); //方法发现异常后不进行处理,直接抛到方法外,由调用者进行异常处理
        }
        return a/b;
    }
}

四、自定义异常

有时候 JDK 提供的异常不能满足我们的需要,我们就需要自己定义一个异常。

我们只需要设计一个异常类,继承自Exception即可。

例:

//自定义一个异常,当输入一个大于10的数时,抛出异常
public class Test {
    public static void main(String[] args) {
        int a = 11;
        try{
            if(a>10){
                //方法内部监听,捕获异常
                throw new TestException(a);
            }
        }catch(TestException e){
            System.out.println(e.toString());
        }
    }
}

//自定义异常
class TestException extends Exception{
    //当数值大于10时触法此异常
    private int detail;

    public TestException(int detail) {
        this.detail = detail;
    }

    @Override
    public String toString() {
        return "TestException{" +
                "detail=" + detail +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐先生没洗头

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

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

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

打赏作者

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

抵扣说明:

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

余额充值