Android基础Java 基础

Java 基础 06. Java 异常

2021-12-13  本文已影响0人  yjtuuige

1. 什么是异常

要理解 Java 异常处理是如何工作的,需要掌握以下三种类型的异常:

2. 异常体系结构

Error

Exception

Error 和 Exception 的区别:

3. Java 异常处理机制与处理异常

package com.xxx.exception;

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

        System.out.println(a / b);
    }
}
package com.xxx.exception;

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

        try {   // try 监控区域
            System.out.println(a / b);
        } catch (ArithmeticException e) {   // catch(想要捕获的异常类型) 捕获异常
            System.out.println("程序出现错误,变量b不能为0");
        } finally { // 处理善后工作 (一般处理IO 资源 关闭)
            System.out.println("finally");
        }
    }
}
package com.xxx.exception;

public class Test01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        // 捕获多个异常:异常类需从小到大
        try {   // try 监控区域
            System.out.println(a / b);
        } catch (Error e) {   // catch(想要捕获的异常类型)  捕获异常
            System.out.println("Error");
        } catch (Exception e) {
            System.out.println("Exception");
        } catch (Throwable e) {
            System.out.println("Throwable");
        } finally { // 处理善后工作 (一般处理IO 资源 关闭)
            System.out.println("finally");
        }
    }
}
package com.xxx.exception;

public class Test02 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        // 快捷键:选中需要捕获的代码 Ctrl + Alt + T
        try {
            System.out.println(a / b);
        } catch (Exception e) {
            e.printStackTrace();    // 打印错误的栈信息
        } finally {
        }
    }
}
package com.xxx.exception;

public class Test03 {
    public static void main(String[] args) {
        new Test03().test(1, 0);
    }

    public void test(int a, int b) {
        if (b == 0) {
            throw new ArithmeticException();    // 主动抛出异常 throw,一般在方法中使用
        }
    }
}
package com.xxx.exception;

public class Test03 {
    public static void main(String[] args) {
        // 捕获异常
        try {
            new Test03().test(1, 0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
    // 假设这个方法中处理不了这个异常,方法上抛出异常 throws
    public void test(int a, int b) throws ArithmeticException {
        if (b == 0) {
            throw new ArithmeticException();    // 主动抛出异常 throw,一般在方法中使用
        }
    }
}

4. 自定义异常

package com.xxx.exception.demo02;

// 自定义的异常类,需要继承 Exception
public class MyException extends Exception {
    // 传递数字>10
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }

    // toString (Alt + Insert): 异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + detail + '}';
    }
}
package com.xxx.exception.demo02;

public class Test {
    // 可能会存在的方法
    static void test(int a) throws MyException {
        System.out.println("传递的参数为:" + a);
        if (a > 10) {
            throw new MyException(a);   // 抛出异常
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(16);
        } catch (MyException e) {
//            e.printStackTrace();
            System.out.println("MyException=>" + e);
        }
    }
}

5. 总结

上一篇下一篇

猜你喜欢

热点阅读