|NO.Z.00065|——————————|BigDataEnd|——|Java&集合类库.V01|——|Java.v01|异常机制.v01|异常机制|概念|分类结构|

一、[异常机制基本概念]——[异常机制的分类和结构]
### --- 基本概念

——>        异常就是"不正常"的含义,在Java语言中主要指程序执行中发生的不正常情况。
——>        java.lang.Throwable类是Java语言中错误(Error)和异常(Exception)的超类。
——>        其中Error类主要用于描述Java虚拟机无法解决的严重错误,
——>        通常无法编码解决,如:JVM挂掉了等。
——>        其中Exception类主要用于描述因编程错误或偶然外在因素导致的轻微错误,
——>        通常可以编码解决,如:0作为除数等。
二、异常的分类
### --- 异常的分类

——>    java.lang.Exception类是所有异常的超类,主要分为以下两种:
——>        RuntimeException - 运行时异常,也叫作非检测性异常
——>        IOException和其它异常 - 其它异常,也叫作检测性异常,
### --- 所谓检测性异常就是指在编译阶段都能被编译器检测出来的异常。

——>    其中RuntimeException类的主要子类:
——>        ArithmeticException类 - 算术异常
——>        ArrayIndexOutOfBoundsException类 - 数组下标越界异常
——>        NullPointerException - 空指针异常
——>        ClassCastException - 类型转换异常
——>        NumberFormatException - 数字格式异常
### --- 注意:

——>        当程序执行过程中发生异常但又没有手动处理时,则由Java虚拟机采用默认方式处理异常,
——>        而默认处理方式就是:打印异常的名称、异常发生的原因、异常发生的位置以及终止程序。
三、异常的框架结构
四、编程代码
package com.yanqi.task16;

public class ExceptionTest {

    public static void main(String[] args) {

        // 1.见识一下非检测性异常  运行时异常
        System.out.println(5 / 0); // 编译ok,运行阶段会发生算术异常  下面的代码不会执行

        // 2.检测性异常
        //Thread.sleep(1000); // 编译错误  不处理就无法到运行阶段

        System.out.println("程序正常结束了!");
    }
}
五、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58512:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task16.ExceptionTest
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.yanqi.task16.ExceptionTest.main(ExceptionTest.java:8)

Process finished with exit code 1

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-04 15:10  yanqi_vip  阅读(29)  评论(0)    收藏  举报

导航