异常处理

一、异常的定义

/*
    异常:java程序编译或运行过程中出现的问题
    Throwable:
        Error: 表示非常严重的问题,自己无法解决的问题
        Exception:
            除了RuntimeException其它异常【编译时期异常】: 一般指的是异常尚未处理就编译了
            RuntimeException【运行时期异常】: 运行过程中报错了,一般情况下是因为语法逻辑不正确导致

    JVM遇到问题时,默认的处理方案是:停止程序,抛出错误。
 */
//class Demo implements Cloneable{
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
//}

public class ExceptionDemo1 {
    public static void main(String[] args) {
//        Demo demo = new Demo();
//        Object clone = demo.clone();

//        int[] arr = {11,22,33,44};
//        System.out.println(arr[5]);

        int i = 5;
        int j = 0;
        System.out.println(i/j);

        System.out.println("hello world");

    }
}

二、异常的处理方案


/*
    异常的处理方案:
        1、try...catch...finally

    try...catch使用注意事项:
        1、当try中出现异常时,JVM会对应创建一个异常类对象
        2、自上而下会与catch中的异常进行匹配,若匹配上就相当于处理了,执行catch中的逻辑
        3、若try中有多个异常,当第一个异常触发时,try中的其它后续代码都不会执行,JVM会对应创建一个异常类对象进行第二步匹配
        4、可以直接写一个catch里面是所有异常的父类、
        5、若存在多个catch匹配,需要将父类异常往后写
        6、若使用jdk新特性的写法的话,异常类之间不能存在继承关系
 */
public class ExceptionDemo2 {
    public static void main(String[] args) {

        int[] arr = {11, 22, 33, 44, 55};

//        try {
//            System.out.println(arr[8]); // new ArrayIndexOutOfBoundsException()
//            System.out.println(10/3); // new ArithmeticException()
//            System.out.println("江川比李刚帅!");
//        }catch (ArrayIndexOutOfBoundsException e){ // = new ArrayIndexOutOfBoundsException()
//            System.out.println("出错啦!!");
//        }catch (ArithmeticException e){
//            System.out.println("除0错误!!");
//        }

//        try {
//            System.out.println(arr[2]); // new ArrayIndexOutOfBoundsException()
//            System.out.println(10/0); // new ArithmeticException()
//            System.out.println("江川比李刚帅!");
//        }catch (ArithmeticException e){
//            System.out.println("除0错误!!");
//        }catch (Exception e){ // = new ArrayIndexOutOfBoundsException()
//            System.out.println("出错啦!!");
//        }

        try {
            System.out.println(arr[8]); // new ArrayIndexOutOfBoundsException()
            System.out.println(10 / 3); // new ArithmeticException()
            System.out.println("江川比李刚帅!");
        } catch (ArrayIndexOutOfBoundsException | ArithmeticException e) { // = new ArrayIndexOutOfBoundsException()
            System.out.println("出错啦!!");
        }


        System.out.println("李刚真帅!");


    }
}

三、try...catch...finally使用

/*
    try...catch...finally

    无论try中的代码是否出错,finally都会执行。
    finally一般来说,用作释放资源的作用。
 */
public class ExceptionDemo4 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55};


        try {
            System.out.println(3/2);// new ArrayIndexOutOfBoundsException()
        } catch (Exception e) { // = new ArrayIndexOutOfBoundsException()
            e.printStackTrace();
        } finally {
            System.out.println("数加666");
        }


        System.out.println("李刚是世界首富!!");
    }
}

四、throws和throw

import java.io.FileInputStream;
import java.io.IOException;

/*
    throws: 抛出
    在方法的定义上抛出异常类, throws本身是不会处理异常的,只是当时方法内部不用处理了,
    但是当调用者调用有问题的方法时,需要调用者进行处理

    throw: 抛出
    在方法内部抛出异常对象,表示一个确定的异常

    注意:
        当调用者调用抛出运行时期异常的方法时,可以选择不去处理,也能运行。
        当调用者调用抛出编译时期异常的方法是,必须要进行处理,否则编译不通过无法运行。

   小故事理解 throw throws try...catch 的使用:
        江川是一个快乐的小矿工,有一天他挖到了一个远古炸弹💣【throw】,往洞外运输【throws】,运输给了矿厂管理人员
        管理员对炸弹💣进行处理【try..catch】

 */
public class ThrowsDemo1 {
    public static void main(String[] args){
//        try {
//            fun1();
//        }catch (Exception e){
//            e.printStackTrace();
//        }

//        fun1();
//
//        System.out.println("hello world");

//        try {
//            fun2();
//        }catch (Exception e){
//            e.printStackTrace();
//        }

//        fun1();

        try{
            fun3();
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    public static void fun3() throws Exception{
        int i= 0;
        if(i==0){
            throw new ArithmeticException();
        }else {
            System.out.println(5/i);
        }
    }

    public static void fun2() throws IOException {
        FileInputStream fis = new FileInputStream("");
    }


    public static void fun1() throws ArithmeticException{
        int[] arr = {11, 22, 33, 44, 55};


        System.out.println(3 / 0);// new ArrayIndexOutOfBoundsException()


        System.out.println("李刚是世界首富!!");
    }
}

posted @ 2024-10-19 12:18  你的镁偷走了我的锌  阅读(41)  评论(0)    收藏  举报