zoukankan      html  css  js  c++  java
  • 十个常见的Java异常出现原因

    异常是 Java 程序中经常遇到的问题,我想每一个 Java 程序员都讨厌异常,一 个异常就是一个 BUG,就要花很多时间来定位异常问题。


    1、NullPointerException

    空指针异常,操作一个 null 对象的方法或属性时会抛出这个异常。具体看上篇文章:空指针常见案例

    2、OutofOutofMemoryError

    内存出现异常的一种异常,这不是程序能控制的,是指要分配的对象的内存超出了当前最大的堆内存,需要调整堆内存大小(-Xmx)以及优化程序。

    3、IOException

    IO,即:input, output,我们在读写磁盘文件、网络内容的时候经常会生的一种异常,这种异常是受检查异常,需要进行手工捕获。

    如文件读写会抛出 IOException:

    1 public int read() throws IOException
    2 public void write(int b) throws IOException

    4、FileNotFoundException

    文件找不到异常,如果文件不存在就会抛出这种异常。

    如定义输入输出文件流,文件不存在会报错:

    1 public FileInputStream(File file) throws FileNotFoundException
    2 public FileOutputStream(File file) throws FileNotFoundException

    FileNotFoundException 其实是 IOException 的子类,同样是受检查异常,需要进行手工捕获。

    5、ClassNotFoundException

    类找不到异常,Java开发中经常遇到,是不是很绝望?这是在加载类的时候抛出来的,即在类路径下不能加载指定的类。

    看一个示例:

    1 public static <T> Class<T> getExistingClass(ClassLoader classLoader, String className) {
    2   try {
    3      return (Class<T>) Class.forName(className, true, classLoader);
    4   }
    5   catch (ClassNotFoundException e) {
    6      return null;
    7   }
    8 }

    它是受检查异常,需要进行手工捕获。

    6、ClassCastException

    类转换异常,将一个不是该类的实例转换成这个类就会抛出这个异常。

    如将一个数字强制转换成字符串就会报这个异常:

    1 Object x = new Integer(0);
    2 System.out.println((String)x);

    这是运行时异常,不需要手工捕获。

    7、NoSuchMethodException

    没有这个方法异常,一般发生在反射调用方法的时候,如:

    1 public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException {
    2     checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
    3     Method method = getMethod0(name, parameterTypes, true);
    4     if (method == null) {
    5         throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
    6     }
    7     return method;
    8 }

    它是受检查异常,需要进行手工捕获。

    8、IndexOutOfBoundsException

    索引越界异常,当操作一个字符串或者数组的时候经常遇到的异常。

    例:一个ArrayList数组中没有元素,而你想获取第一个元素,运行是就会报此类型的错误。

    public class test{
         public static void main(args[] ){
             List<String> list = new ArrayList<>();
             System.out.println(list.get(0));
       }
    }                  

    它是运行时异常,不需要手工捕获。

    9、ArithmeticException

    算术异常,发生在数字的算术运算时的异常,如一个数字除以 0 就会报这个错。

    1 double n = 3 / 0;

    这个异常虽然是运行时异常,可以手工捕获抛出自定义的异常,如:

    1 public static Timestamp from(Instant instant) {
    2     try {
    3         Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
    4         stamp.nanos = instant.getNano();
    5         return stamp;
    6     } catch (ArithmeticException ex) {
    7         throw new IllegalArgumentException(ex);
    8     }
    9 }

    10、SQLException

    SQL异常,发生在操作数据库时的异常。

    如下面的获取连接:

    1 public Connection getConnection() throws SQLException {
    2     if (getUser() == null) {
    3         return DriverManager.getConnection(url);
    4     } else {
    5         return DriverManager.getConnection(url, getUser(), getPassword());
    6     }
    7 }

    又或者是获取下一条记录的时候:

    1 boolean next() throws SQLException;

    它是受检查异常,需要进行手工捕获。

    这里只列举了 10 个 Java 中最常见的基本异常

     

  • 相关阅读:
    luffy后台登录+注册+课程
    luffy前台登录+注册+课程
    luffy前台准备
    luffy后台准备
    跨域请求
    pip源和虚拟环境的搭建
    Book接口
    drf-Xadmin的使用
    drf-JWT认证
    drf-自动生成接口文档
  • 原文地址:https://www.cnblogs.com/aiitzzx0116/p/10505476.html
Copyright © 2011-2022 走看看