• java部分常见错误示例


    Java中较为复杂和常见的错误示例,包括运行后的错误信息以及修复方法:

    1. 空指针异常(NullPointerException)

    1. String text = null;
    2. int length = text.length(); // 运行后会抛出 NullPointerException

    错误信息:

    1. Exception in thread "main" java.lang.NullPointerException
    2. at MyClass.main(MyClass.java:3)

    修复方法:在使用变量前,检查其是否为null。

    1. String text = null;
    2. if (text != null) {
    3. int length = text.length();
    4. } else {
    5. System.out.println("text is null");
    6. }

    2. 数组越界异常(ArrayIndexOutOfBoundsException)

    1. int[] numbers = {1, 2, 3};
    2. int value = numbers[3]; // 运行后会抛出 ArrayIndexOutOfBoundsException

    错误信息:

    1. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    2. at MyClass.main(MyClass.java:3)

    修复方法:确保访问的数组索引在有效范围内。

    1. int[] numbers = {1, 2, 3};
    2. if (numbers.length > 3) {
    3. int value = numbers[3];
    4. } else {
    5. System.out.println("Invalid index");
    6. }

    3. 类型转换异常(ClassCastException)

    1. Object obj = "Hello";
    2. Integer num = (Integer) obj; // 运行后会抛出 ClassCastException

    错误信息:

    1. Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer
    2. at MyClass.main(MyClass.java:3)

    修复方法:确保对象可以进行类型转换

    1. Object obj = "Hello";
    2. if (obj instanceof Integer) {
    3. Integer num = (Integer) obj;
    4. } else {
    5. System.out.println("Cannot cast to Integer");
    6. }

    4. 除零异常(ArithmeticException)

    int result = 5 / 0;  // 运行后会抛出 ArithmeticException
    

    错误信息:

    1. Exception in thread "main" java.lang.ArithmeticException: / by zero
    2. at MyClass.main(MyClass.java:3)

    修复方法:避免除数为零。

    1. int divisor = 2;
    2. if (divisor != 0) {
    3. int result = 5 / divisor;
    4. } else {
    5. System.out.println("Cannot divide by zero");
    6. }

    5. 文件未找到异常(FileNotFoundException)

    1. File file = new File("nonexistent.txt");
    2. Scanner scanner = new Scanner(file); // 运行后会抛出 FileNotFoundException

    错误信息:

    1. Exception in thread "main" java.io.FileNotFoundException: nonexistent.txt (No such file or directory)
    2. at java.base/java.io.FileInputStream.open0(Native Method)
    3. at java.base/java.io.FileInputStream.open(FileInputStream.java:220)
    4. at java.base/java.io.FileInputStream.(FileInputStream.java:158)
    5. at java.base/java.util.Scanner.(Scanner.java:639)
    6. at MyClass.main(MyClass.java:3)

    修复方法:确保文件存在或捕获异常。

    1. File file = new File("nonexistent.txt");
    2. try {
    3. Scanner scanner = new Scanner(file);
    4. // 读取文件内容
    5. } catch (FileNotFoundException e) {
    6. System.out.println("File not found: " + e.getMessage());
    7. }

  • 相关阅读:
    Spring IoC 入门学习笔记
    java指令重排序
    中秋不加班,猿人永不屈服!!! So,How to celebrate the Mid Autumn Festival?
    Nginx的安装及启动【编译安装+Docker拉取安装(开发时首选)】
    RHEL8.4 安装docker&docker-compose
    数据科学与大数据(学习记录)
    要有遥不可及的梦想,也要有脚踏实地的本事
    【SpringCloud】一、SpringCloud介绍
    【山东科技大学OJ】1163 Problem A: 时间:24小时制转12小时制
    Ant Design Modal模态框添加类名后样式不生效
  • 原文地址:https://blog.csdn.net/m0_62110645/article/details/133501664