认识Java中的异常处理

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Java异常处理

异常体系的介绍

什么事异常处理?

异常是程序在“编译”或者“执行”的过程中可能出现的问题比如: 数组索引越界、空指针异常、 日期格式化异常等…

注意语法错误不算在异常体系中。

为什么要学习异常?

异常一旦出现了如果没有提前处理程序就会退出JVM虚拟机而终止.

研究异常是为了避免异常然后提前处理异常体现的是程序的安全, 健壮性。

在这里插入图片描述

Error是系统级别问题、JVM退出等代码无法控制

Exceptionjava.lang包下称为异常类它表示程序本身可以处理的问题

  • RuntimeException及其子类统称为运行时异常编译阶段不会报错。 (空指针异常数组索引越界异常)
  • 除RuntimeException之外所有的异常统称为编译时异常编译期必须处理的否则程序不能通过编译。 (日期格式化异常)。

编译时异常和运行时异常:

在这里插入图片描述

简单来说:

编译时异常就是在编译的时候出现的异常

运行时异常就是在运行时出现的异常。

常见运行时异常

运行时异常;

直接继承自RuntimeException或者其子类编译阶段不会报错运行时可能出现的错误。

常见的运行时异常示例:

数组索引越界异常: ArrayIndexOutOfBoundsException

public static void main(String[] args) {
    // 数组索引越界异常: ArrayIndexOutOfBoundsException
    int[] arr = {10, 20, 40};
    System.out.println(arr[0]);
    System.out.println(arr[1]);
    System.out.println(arr[2]);
    System.out.println(arr[3]); // 越界

    // 异常提示: 
    // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    //	at com.chenyq.d6_exception.ExceptionDemo.main(ExceptionDemo.java:11)
}

空指针异常 : NullPointerException直接输出没有问题但是调用空指针的变量的功能就会报错。

public static void main(String[] args) {
    String name = null;
    System.out.println(name.length()); // 空指针异常

    // 异常信息:
    // Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null
    //	at com.chenyq.d6_exception.ExceptionDemo2.main(ExceptionDemo2.java:6)
}

数学操作异常ArithmeticException

public static void main(String[] args) {
    int c = 20 / 0; // 数学异常, 分母不能为0

    // 异常信息:
    // Exception in thread "main" java.lang.ArithmeticException: / by zero
    //	at com.chenyq.d6_exception.ExceptionDemo4.main(ExceptionDemo4.java:5)
}

类型转换异常ClassCastException

public static void main(String[] args) {
        Object obj = 12;
    String str = (String) obj; // 类型转换异常

    // 异常信息
    // Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class
    // java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
    //	at com.chenyq.d6_exception.ExceptionDemo3.main(ExceptionDemo3.java:6
}

数字转换异常 NumberFormatException

public static void main(String[] args) {
    String num1 = "23"; // 可以正常转换
    Integer res1 = Integer.valueOf(num1);
    System.out.println(res1 + 1);

    String num2 = "23aaa"; // 不可以正常转换 数字转换异常
    Integer res2 = Integer.valueOf(num2);
    System.out.println(res2 + 1);

    // 异常信息
    // Exception in thread "main" java.lang.NumberFormatException: For input string: "23aaa"
    //	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    //	at java.base/java.lang.Integer.parseInt(Integer.java:668)
    //	at java.base/java.lang.Integer.valueOf(Integer.java:999)
    //	at com.chenyq.d6_exception.ExceptionDemo5.main(ExceptionDemo5.java:10)
}

运行时异常一般是程序员业务没有考虑好或者是编程逻辑不严谨引起的程序错误自己水平问题

常见编译时异常

编译时异常:

不是RuntimeException或者其子类的异常编译阶段就报错(代码写出来就报错)必须处理否则代码不通过

编译时异常示例:

在这里插入图片描述

编译时异常的作用是什么

是担心程序员的技术不行在编译阶段就爆出一个错误, 目的在于提醒代码可能有错误

编译时异常是可遇不可求, 遇到了解决即可。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java