【java】异常

异常概述

在这里插入图片描述

JVM的默认处理方案

在这里插入图片描述

异常处理 try…catch…

在这里插入图片描述

作用:使程序在处理异常的同时,也可以往下执行

package heima;

public class P215 {
    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();
        }
    }
}

在这里插入图片描述

Throwable成员方法

在这里插入图片描述
三种方法的演示,输出在//后面:

package heima;

public class P216 {
    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){
//            e.printStackTrace();

            //getMessage() 返回此throwable的详细消息字符串。
//            System.out.println(e.getMessage()); //3

            //toString() 返回此可抛出的简短描述。
//            System.out.println(e.toString());//java.lang.ArrayIndexOutOfBoundsException: 3
        }
    }
}

编译时异常和运行时异常

在这里插入图片描述

  • 运行时异常
package heima;

public class P217 {
    public static void main(String[] args) {
        method();
    }
    //运行时异常
    public  static void method() {
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);
        } catch (ArrayIndexOutOfBoundsException e) { //因为该异常类的父类是RuntimeException
//            e.printStackTrace();
        }
    }
}

原因:该异常类的父类是RuntimeException
在这里插入图片描述

  • 编译时异常

编译阶段就会报错:
在这里插入图片描述
鼠标停在上方,发现问题:
在这里插入图片描述
同样进行抛出处理,编译正常:

package heima;

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

public class P217 {
    public static void main(String[] args) {
        method2();
    }
    //编译时异常
    public static void method2(){
        try {
            String s = "2022-11-13";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
            Date d = sdf.parse(s);
            System.out.println(d);
        }catch (ParseException e){
            e.printStackTrace();
        }
    }
}

异常处理 throws

在这里插入图片描述

  • 对运行时异常 --没有效果
public static void main(String[] args) {
        System.out.println("开始");
        method1();
        System.out.println("结束");
    }
    
public static void method1() throws ArrayIndexOutOfBoundsException{
        int[] arr = {1,2,3};
        System.out.println(arr[3]);
    }

在这里插入图片描述

结果同没抛出前

  • 对编译时异常 --想继续执行,依然要使用try-catch

只不过使用throws,可以抛给调用者处理

package heima;

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

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

    //运行时异常 try-catch
    public  static void method() {
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);
        } catch (ArrayIndexOutOfBoundsException e) { //因为该异常类的父类是RuntimeException
//            e.printStackTrace();
        }
    }
    //运行时异常 throws
    public static void method1() throws ArrayIndexOutOfBoundsException{
        int[] arr = {1,2,3};
        System.out.println(arr[3]);
    }
}

自定义异常

在这里插入图片描述

  • 无参构造

ScoreException.java

package heima.P219;

public class ScoreException extends Exception {
    public ScoreException() {}
    public ScoreException(String message){
        super(message);
    }
}

Teacher.java

package heima.P219;

public class Teacher {
    public void checkScore(int score) throws ScoreException{
        if(score<0 || score>100){
            throw new ScoreException();
        }else {
            System.out.println("分数正常");
        }
    }
}

TeacherTest.java

package heima.P219;

import java.util.Scanner;

public class TeacherTest {
    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();
        }
    }
}

输出:
在这里插入图片描述

  • 自定义提示信息 --有参构造

改动Teacher.java

package heima.P219;

import javax.script.ScriptException;

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("分数正常");
        }
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值