Java基础学习之异常(17)

1. 异常(Exception)

1.1 异常概述

异常:就是程序出现了不正常的情况
在这里插入图片描述

1.2 JVM 默认处理方案

如果程序出现了问题,JVM会做默认的处理

  • 把异常的名称,异常的原因及异常出现的位置等信息输出在控制台。
  • 程序停止执行
package Java17.YiChang;

//异常
public class demo1 {
    public static void main(String[] args) {
        System.out.println("开始");
        method();
        System.out.println("结束");
    }
    public static void method(){
        int[] arr = {1,2,3};
//        System.out.println(arr[1]);
        System.out.println(arr[3]);//ArrayIndexOutOfBoundsException数组索引越界异常

    }
}
 /*   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at Java17.YiChang.demo1.method(demo1.java:13)
        at Java17.YiChang.demo1.main(demo1.java:7)*/

1.3 异常处理

两种方案

  • try------catch
    格式:
try{
	可能出现异常的代码;
}catch(异常类名 变量名){
	异常的处理代码
}
package Java17.YiChang;

public class demo2 {
    public static void main(String[] args){
        System.out.println("开始");
        method();
        System.out.println("结束");
    }
    public static void method(){
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);
        }catch(ArrayIndexOutOfBoundsException e){
//            System.out.println("你访问的数组索引不存在");
            e.printStackTrace();
        }
    }
}

  • throws

1.5 Throwable 的成员方法

在这里插入图片描述

package Java17.YiChang;


public class demo3 {
    public static void main(String[] args) {
        System.out.println("开始");
        method();
        System.out.println("结束");
    }

    public static void method() {
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException("xxx")
        } catch (ArrayIndexOutOfBoundsException e) { //e就是一个对象
//            e.printStackTrace();
//            System.out.println(e.getMessage());
//            System.out.println(e.toString());
            //java.lang.ArrayIndexOutOfBoundsException: 3
            e.printStackTrace();
        }
    }
}
/*
public class Throwable{
        private String detailMessage;

    public Throwable(String message){
        detailMessage = message;
    }
    public String getMessage() {
        return detailMessage;
    }
}
    * */

1.6 编译时异常和运行时异常的区别

Java异常分为两大类:编译时异常运行时异常,也被称为受检异常非受检异常
所有RuntimeException类及其子类被称为运行时异常,其他异常都是编译时异常

  • 编译时异常:必须显示处理,否则程序就会发生错误无法用过编译
  • 运行时异常:无需显示处理,也可以和编译时异常一样处理
import java.util.Date;

//编译时异常和运行时异常
public class demo {
    public static void main(String[] args) {
//        method();
        method2();
    }
    //编译时异常
    public static void method2(){
        try {
            String s = "2022-08-09";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse(s);
            System.out.println(d);
        }catch(ParseException e){
            e.printStackTrace();
        }
    }
    //运行时异常
    public static void method(){
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);//ArrayIndexOutOfBoundsException
        }catch(ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        }
    }

}

1.7 throws处理异常

try–catch 可以对异常进行处理,但并不是所有情况我们都有权限进行异常的处理,针对这种情况Java提供了throws的处理方案
格式:

throws 异常类名;
package Java17.YiChang;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//throws处理异常
public class demo4 {
    public static void main(String[] args) {
        System.out.println("开始");
//        method();\
        try {
            method2();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("结束");
    }
    //编译时异常
    public static void method2() throws ParseException {
        String s = "2022-12-12";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(s);
        System.out.println(d);
    }

    //运行时异常
    public static void method() throws ArrayIndexOutOfBoundsException{
        int[] arr = {1,2,3};
        System.out.println(arr[3]);
    }
}
  • 编译时异常必须进行处理,两种处理方案:try — catch或者throws,如果采用throws这种方案,将来谁调用谁来处理,相当于只是延迟了异常处理,如method2()方法所示,调用后还是需要继续处理异常。
  • 运行时异常可以不处理,出现问题后,需要我们回来修改代码

1.8 自定义异常

只要我们的类继承了RuntimeException或者Exception,它就变成了异常体系的一员。
格式:

public class 异常类名 extends Exception{
	无参构造
	带参构造
}
package Java17.YiChang;

public class ScoreException extends Exception{
    public ScoreException(){}

    public ScoreException(String massage){
        //super 可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类。
        //super 相当于是指向当前对象的父类,这样就可以用 super.xxx 来引用父类的成员或方法。
        //super(参数):调用父类中的某一个构造函数
        super(massage);
    }

}

package Java17.YiChang;

public class Teacher {
    public void checkScore(int score) throws ScoreException{
        if(score<0 ||score>100){
//            throw new ScoreException();
            throw new ScoreException("你给出的分数有误,分数应该在0-100之间");
        }else{
            System.out.println("分数正常");
        }
    }
}
package Java17.YiChang;

import java.util.Scanner;

public class Teachertext {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入分数");
        int score = sc.nextInt();

        Teacher t = new Teacher();
        try {
            t.checkScore(score);
        } catch (ScoreException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值