zoukankan      html  css  js  c++  java
  • 10 个深恶痛绝的 Java 异常

    1、NullPointerException

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

    2、OutOfMemoryError

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

    3、IOException

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

    如文件读写会抛出 IOException:

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

    4、FileNotFoundException

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

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

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

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

    5、ClassNotFoundException

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

    看一个示例:

    public static <T> Class<T> getExistingClass(ClassLoader classLoader, String className) {
      try {
         return (Class<T>) Class.forName(className, true, classLoader);
      }
      catch (ClassNotFoundException e) {
         return null;
      }
    }

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

    6、ClassCastException

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

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

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

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

    7、NoSuchMethodException

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

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

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

    8、IndexOutOfBoundsException

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

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

    9、ArithmeticException

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

    double n = 3 / 0;

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

    public static Timestamp from(Instant instant) {
        try {
            Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
            stamp.nanos = instant.getNano();
            return stamp;
        } catch (ArithmeticException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    10、SQLException

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

    如下面的获取连接:

    public Connection getConnection() throws SQLException {
        if (getUser() == null) {
            return DriverManager.getConnection(url);
        } else {
            return DriverManager.getConnection(url, getUser(), getPassword());
        }
    }

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

    boolean next() throws SQLException;

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

  • 相关阅读:
    networkX用法整
    在人生路上对我影响最大的三位老师
    介绍自己
    介绍自己
    自我介绍
    打印沙漏1
    介绍自己
    对我影响最大的三位老师
    人生路上影响对我最大的三位老师
    1.自我介绍
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10394739.html
Copyright © 2011-2022 走看看