/* 看板娘 */ canvas#live2dcanvas { border: 0 !important; left: 0; }

Java 中的异常

目录

  1、异常的概述

  2、JVM默认处理异常的方式

  3、try-catch方式处理异常

  4、Throwable成员方法

  5、编译时异常和运行时异常的区别

  6、throws方式处理异常

  7、throws和throw的区别

  8、自定义异常

1、异常

  异常的概述

  异常就是程序出现了不正常的情况

  异常的体系结构

2、JVM默认处理异常的方式

  如果程序出现了问题,我们没有做任何处理,最终JVM 会做默认的处理,处理方式有如下两个步骤:

  把异常的名称,错误原因及异常出现的位置等信息输出在了控制台

  程序停止执行 

package com.itheima_01;

/*
    JVM的默认处理方案
 */
public class ExceptionDemo02 {
    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]);
    }
}
/*
开始
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    at com.itheima_01.ExceptionDemo02.method(ExceptionDemo02.java:16)
    at com.itheima_01.ExceptionDemo02.main(ExceptionDemo02.java:9)
 */

3、try-catch方式处理异常

定义格式

执行流程

  程序从 try 里面的代码开始执行

  出现异常,就会跳转到对应的 catch 里面去执行

  执行完毕之后,程序还可以继续往下执行

示例代码

package com.itheima_02;

/*
    try {
        可能出现异常的代码;
    } catch(异常类名 变量名) {
        异常的处理代码;
    }
 */
public class ExceptionDemo01 {
    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]);
            System.out.println("这里能够访问到吗");
        } catch (ArrayIndexOutOfBoundsException e) {
//            System.out.println("你访问的数组索引不存在,请回去修改为正确的索引");
            e.printStackTrace();
        }
    }
}

4、Throwable成员方法

常用方法

示例代码

package com.itheima_02;

/*
    public String getMessage():返回此 throwable 的详细消息字符串
    public String toString():返回此可抛出的简短描述
    public void printStackTrace():把异常的错误信息输出在控制台
 */
public class ExceptionDemo02 {
    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();
            System.out.println("这里能够访问到吗");
        } catch (ArrayIndexOutOfBoundsException e) { //new ArrayIndexOutOfBoundsException();
//            e.printStackTrace();

            //public String getMessage():返回此 throwable 的详细消息字符串
//            System.out.println(e.getMessage());
            //Index 3 out of bounds for length 3

            //public String toString():返回此可抛出的简短描述
//            System.out.println(e.toString());
            //java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

            //public void printStackTrace():把异常的错误信息输出在控制台
            e.printStackTrace();
//            java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
//            at com.itheima_02.ExceptionDemo02.method(ExceptionDemo02.java:18)
//            at com.itheima_02.ExceptionDemo02.main(ExceptionDemo02.java:11)


        }
    }
}

/*
    public class Throwable {
        private String detailMessage;

        public Throwable(String message) {
            detailMessage = message;
        }

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

5、编译时异常和运行时异常的区别

   编译时异常

    都是Exception类及其子类

    必须显示处理,否则程序就会发生错误,无法通过编译

  运行时异常

    都是RuntimeException类及其子类

    无需显示处理,也可以和编译时异常一样处理 

package com.itheima_02;

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

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

    编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译
    运行时异常:无需显示处理,也可以和编译时异常一样处理
 */
public class ExceptionDemo03 {
    public static void main(String[] args) {
//        method();
        method2();
    }

    //编译时异常
    public static void method2() {
        try {
            String s = "2048-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]);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

}

6、throws方式处理异常

定义格式

示例代码

package com.itheima_03;

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

/*
    throws 异常类名;
    这个格式是跟在方法的括号后面的
 */
public class ExceptionDemo {
    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 = "2048-08-09";
        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]);
    }

}

注意事项

  这个throws格式是跟在方法的括号后面的

  编译时异常必须要进行处理,两种处理方案:try...catch …或者 throws,如果采用 throws 这种方案, 将来谁调用谁处理

  运行时异常可以不处理,出现问题后,需要我们回来修改代码 

7、throws和throw的区别

8、自定义异常

自定义异常类

 

 示例代码

package com.itheima_04;

public class ScoreException extends Exception {

    public ScoreException() {}

    public ScoreException(String message) {
        super(message);
    }

}


// --------------------------------------------------------------------------

package com.itheima_04;

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 com.itheima_04;

import java.util.Scanner;

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

 

posted @ 2021-08-24 16:05  群临天下  阅读(48)  评论(0编辑  收藏  举报
/* 看板娘 */