Java 无法抛出异常,为什么?

Java 无法抛出异常,为什么?,java,exception-handling,Java,Exception Handling,在第一个catch块中,为什么我们不能抛出异常对象?这里的RuntimeException工作正常 public class CirEx { public Circle getCircle(int id) { Connection conn = null; try { Class.forName(""); conn = DriverManager.getConnection("");

在第一个catch块中,为什么我们不能抛出
异常
对象?这里的
RuntimeException
工作正常

public class CirEx {
    public Circle getCircle(int id) {
        Connection conn = null;
        try {
            Class.forName("");
            conn = DriverManager.getConnection("");
            PreparedStatement pstmt = conn.prepareStatement("");

            Circle circle = new Circle(1, "");
            return circle;
        } catch (Exception e) {
            throw new RuntimeException(e);
            // why we cann't do that.
            // throw new Exception(e);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }
    }
}

我们可以抛出
异常
,只要我们声明抛出相同的
异常
抛出异常
子句)或处理它(使用
try catch
块)

异常
是一个检查异常,必须对其进行处理

但是

RuntimeException
之所以有效,是因为它的未检查异常,因此我们不需要使用
抛出
子句或处理它


因为在这种情况下,您必须声明方法引发异常

 public Circle getCircle(int id) throws Exception{
Connection conn = null;
try {
    Class.forName("");
    conn = DriverManager.getConnection("");
    PreparedStatement pstmt = conn.prepareStatement("");

    Circle circle = new Circle(1, "");
    return circle;
} catch (Exception e) {
    throw new RuntimeException(e);
    // why we cann't do that.
    // throw new Exception(e);
}

finally {
    try {
        conn.close();
    } catch (SQLException e) {
        System.out.println(e);
    }
}
}


注意:RuntimeException及其子类是特殊类型的异常,不需要明确捕获

请使用标题描述您的实际问题。这是一个格式错误的问题(因为标题),这段代码根本没有问题。它怎么不相关?你不必声明运行时异常。这就是我所说的,并在示例中演示了抛出运行时异常是完全可以的,但如果他想抛出异常,就必须声明它。你不需要声明运行时异常。我在示例中提到过吗邮局?为什么要投否决票?我只是想知道我的想法的原因,这就是为什么我问这个问题。