JAVA异常处理(二):异常的分类

142 阅读1分钟

先举个生活中的例子:

出门前:看天气,如果下雨了就不出门了(编译时异常),找到电卡

出门后:等公交-->堵车(运行时异常)
-->发生地震(错误)

到达充电公司:排队上电梯

排队人太多了-->不充了

很快排队队-->充值回家

异常的分类:

1.编译时异常(Exceprion):程序在编译过程中发现的异常,受检异常。

//编译时异常
int i;
System.out.println(i);

2. 运行时异常(RuntimeException):又称为非受检异常。

//2.运行时异常
System.out.println(10/0);

3.错误(Error):由java虚拟机生成并抛出的异常,程序对其不作处理

public class demo01 {
    public static void main(String[] args) {
        /*1.编译时异常
        int i;
        System.out.println(i);*/
        //2.运行时异常
        System.out.println(10/0);
        //3.Error错误,java.lang.OutOfMemoryError
        show();
    }
    public static void show(){
        System.out.println("show...");
        String[] strings = new String[1024 * 1024];//内存都被占用完了
        show();
    }
}

异常的体系结构

image.png