ITPub博客

首页 > 应用开发 > Java > Java 中如何抛出多个异常?

Java 中如何抛出多个异常?

原创 Java 作者:mingtian66 时间:2024-11-10 12:56:13 0 删除 编辑

在 Java 中,可以通过多种方式来抛出和处理多个异常。以下是几种常用的方式:
1. 在方法签名中声明多个异常

如果方法可能抛出不同类型的异常,可以在方法签名中声明多个异常类型,用逗号分隔。

public void multipleExceptions() throws IOException, SQLException {
    // 可能会抛出 IOException
    if (someCondition()) {
        throw new IOException("IO error occurred");
    }

    // 可能会抛出 SQLException
    if (someOtherCondition()) {
        throw new SQLException("SQL error occurred");
    }
}

调用该方法时,调用方需要捕获所有可能的异常类型。

try {
    multipleExceptions();
} catch (IOException | SQLException e) {
    System.out.println("Caught exception: " + e.getMessage());
}

2. 捕获和重新抛出多个异常

在 catch 块中捕获多个异常后,可以将它们重新封装到一个异常中并抛出。这对于不希望方法声明多个异常类型的情况很有帮助。

public void multipleExceptions() throws Exception {
    try {
        if (someCondition()) {
            throw new IOException("IO error occurred");
        }
        if (someOtherCondition()) {
            throw new SQLException("SQL error occurred");
        }
    } catch (IOException | SQLException e) {
        throw new Exception("Multiple exceptions occurred", e);  // 将多个异常包装到新的异常中
    }
}

3. Java 7 的多重捕获(Multi-Catch)

Java 7 及以上版本允许在一个 catch 块中捕获多个异常,用 | 分隔。这样可以避免重复的代码块。

try {
    multipleExceptions();
} catch (IOException | SQLException e) {
    System.out.println("Caught exception: " + e.getMessage());
}

4. 抛出 Throwable 列表

如果方法需要在同一操作中捕获并处理多个异常,可以使用 List<Throwable> 来收集异常,并在最后统一抛出或处理。

public void multipleExceptions() throws Exception {
    List<Exception> exceptions = new ArrayList<>();
    
    try {
        if (someCondition()) {
            throw new IOException("IO error occurred");
        }
    } catch (IOException e) {
        exceptions.add(e);
    }

    try {
        if (someOtherCondition()) {
            throw new SQLException("SQL error occurred");
        }
    } catch (SQLException e) {
        exceptions.add(e);
    }

    if (!exceptions.isEmpty()) {
        Exception combinedException = new Exception("Multiple exceptions occurred");
        for (Exception e : exceptions) {
            combinedException.addSuppressed(e);
        }
        throw combinedException;
    }
}

5. 使用 addSuppressed 抛出多个异常

Throwable 类提供了 addSuppressed 方法,可以将一个异常附加到另一个异常中。这样,当抛出主异常时,可以查看被抑制的其他异常信息。

public void multipleExceptions() throws Exception {
    Exception mainException = null;

    try {
        if (someCondition()) {
            throw new IOException("IO error occurred");
        }
    } catch (IOException e) {
        mainException = e;
    }

    try {
        if (someOtherCondition()) {
            throw new SQLException("SQL error occurred");
        }
    } catch (SQLException e) {
        if (mainException == null) {
            mainException = e;
        } else {
            mainException.addSuppressed(e);
        }
    }

    if (mainException != null) {
        throw mainException;
    }
}

这些方法可以根据不同需求来选择,在方法声明上声明多个异常类型或使用 addSuppressed 是更常用的方式之一。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70040412/viewspace-3048246/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2024-08-07

  • 博文量
    589
  • 访问量
    602998