Java中的几种常见异常

 异常

       在Java中,异常是指在程序执行过程中遇到的错误或异常情况。当程序遇到异常时,它会抛出一个异常对象。Java中定义了许多异常类型,每种异常类型都代表了一种特定的错误或异常情况。下面是一些Java中常见的异常类型及其说明:

1.NullPointerException(空指针异常):当一个已经被引用但没有指向任何对象的变量被使用时,就会抛出这个异常。比如访问一个null对象的属性或方法。

2.ArrayIndexOutOfBoundsException(数组越界异常):当尝试访问数组中不存在的元素时,就会抛出这个异常。

3.ArithmeticException(算术异常):当在进行数学计算时出现错误时,就会抛出这个异常。比如除数为0。

4.FileNotFoundException(文件未找到异常):当尝试打开一个不存在的文件时,就会抛出这个异常。

5.RuntimeException(运行时异常):这是一种非常常见的异常类型,它可以被程序员捕捉并处理,也可以被忽略。

        以上是一些Java中常见的异常类型,当然还有很多其它的异常类型,在程序开发中我们需要根据具体的需求选择合适的异常类型来处理程序异常,Java中的许多异常都继承自RuntimeException,包括NullPointerException和ArrayIndexOutOfBoundsException等。

代码示例

1.NullPointerException

public class NullPointerExample {
    public static void main(String[] args) {
        try {
            String str = null;
            System.out.println(str.length());
        } catch (NullPointerException e) {
            System.out.println("发生了空指针异常);
        }
    }
}

        以上代码中,我们将str变量赋值为null,然后尝试调用它的length()方法。因为变量没有指向任何实际对象,所以会抛出NullPointerException异常。

2.IndexOutOfBoundsException

public class IndexOutOfBoundsExample {
    public static void main(String[] args) {
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("发生了数组越界异常);
        }
    }
}

        以上代码中,我们访问了arr数组中不存在的元素,即第4个元素。因为数组只有3个元素,所以会抛出IndexOutOfBoundsException异常。

3.ArithmeticException

public class ArithmeticExample {
    public static void main(String[] args) {
        try {
            int a = 5;
            int b = 0;
            int result = a / b;
        } catch (ArithmeticException e) {
            System.out.println("发生了算术异常);
        }
    }
}

        以上代码中,我们试图获取两个数的商,但其中一个数为0,会抛出ArithmeticException异常。

4.FileNotFoundException

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class FileNotFoundExample {
    public static void main(String[] args) {
        try {
            File file = new File("test.txt");
            FileInputStream fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            System.out.println("发生了文件未找到异常);
        }
    }
}

        以上代码中,我们试图打开一个不存在的文件test.txt,所以会抛出FileNotFoundException异常。

5.RuntimeException

public class RuntimeExceptionExample {
    public static void main(String[] args) {
        try {
            int a = 5;
            int b = 0;
            int result = a / b;
        } catch (RuntimeException e) {
            System.out.println("发生了运行时异常,具体异常信息为:" + e.getMessage());
        }
    }
}

        以上代码与ArithmeticExample示例非常相似,只是将catch块中的异常类型改为了RuntimeException。因为ArithmeticException是RuntimeException的子类,所以抛出ArithmeticException异常也属于抛出运行时异常。

       

总结

        在上述代码中,我们分别演示了如何抛出和处理NullPointerException、IndexOutOfBoundsException、ArithmeticException和FileNotFoundException异常。当然,我们也可以使用其他类型的异常,并按照相同的方式处理它们。在实际开发中,可能会抛出很多种不同类型的运行时异常,我们可以将它们的父类设为RuntimeException,这样就可以在catch块中捕获并处理这些异常。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值