Java中try-catch-finally抛出异常问题

发布于 2024-09-14 02:57:09 字数 593 浏览 10 评论 0原文

我是 Java 初学者,但我认为在使用 try-catch-finally 时,我不必使用 throws SQLException 声明异常。但是,如果我不使用它,编译器会给我错误:

“未报告的异常 java.sql.SQLException;必须捕获或声明抛出”。

我包含了一个捕获,所以我不确定为什么会发生此错误。

public static ResultSet getResultSet ( String query ) 
{
    dbConn = getConnection();

    try
    {
       stmt = dbConn.createStatement( );

       ResultSet rs = stmt.executeQuery( query );

       return rs;
    }
   catch (SQLException ex)
   {
       return null;
   }
   finally
   {
       stmt.close();
       dbConn.close();
   }
}

I'm Java beginner, but I thought that when using try-catch-finally I don't have to declare the exception using throws SQLException. However if I don't use it the compiler gives me the error:

"unreported exception java.sql.SQLException; must be caught or declare to be thrown".

I included a catch so I'm not sure why this errors occurs.

public static ResultSet getResultSet ( String query ) 
{
    dbConn = getConnection();

    try
    {
       stmt = dbConn.createStatement( );

       ResultSet rs = stmt.executeQuery( query );

       return rs;
    }
   catch (SQLException ex)
   {
       return null;
   }
   finally
   {
       stmt.close();
       dbConn.close();
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼波传意 2024-09-21 02:57:10

这是因为 close() 方法:

stmt.close();
dbConn.close();

可能会抛出 SQLException 并且您没有将它们封装在 try/catch 块中。

方法很可能从finally 子句中抛出异常,并且如果没有catch 子句处理这些异常,则必须声明该方法来抛出这些异常。

基本上,你需要做类似的事情

finally
{
    try {
        stmt.close();
    } catch (SQLException sqle) {
        // log the statement-close exception
    }

    try {
        dbConn.close();
    } catch (SQLException sqle) {
        // log the connection-close exception
    }
}

It's because the close() methods:

stmt.close();
dbConn.close();

may throw SQLException and you haven't encapsulated them in a try/catch block.

A method may very well throw an exception from within a finally-clause, and, with no catch-clause handling those exceptions, the method must be declared to throw those exceptions.

Basically, you need to do something like

finally
{
    try {
        stmt.close();
    } catch (SQLException sqle) {
        // log the statement-close exception
    }

    try {
        dbConn.close();
    } catch (SQLException sqle) {
        // log the connection-close exception
    }
}
凑诗 2024-09-21 02:57:10

dbConn = getConnection(); 放入 try 部分。 它也可以抛出SQLException

Put dbConn = getConnection(); in the try section. It can throw an SQLException too.

深海夜未眠 2024-09-21 02:57:10

除了编译器错误之外,此代码在运行时也会失败。

当您关闭创建 ResultSetConnection 时,ResultSet 将失效,并且对其进行的任何调用都将失败。 (当然,需要实现一些奇怪的 JDBC 驱动程序才能工作,但这是 JDBC API 的错误用法。)

ResultSet 的范围和生命周期不能比其父级 更宽代码>连接语句

Aside from the compiler errors, this code will fail at runtime.

When you close the Connection from which the ResultSet is created, the ResultSet will be invalidated, and any calls on it will fail. (Sure, some strange JDBC driver be implemented so that this could work, but this is incorrect usage of the JDBC API.)

The scope and lifetime of the ResultSet can't be any wider than its parent Connection and Statement.

负佳期 2024-09-21 02:57:10

您编写的代码使用的类具有必须处理的异常类型。您可以通过两种方式之一来做到这一点,将其传递到链上,即重新抛出异常,或者通过将 close 方法放入 finally 中自己的 try catch 中来实际处理它,如其他一些注释所示堵塞。

通常,您会在文件 IO 类中看到这些,您应该始终关闭对象或流以确保将其保留在有效状态。否则,您将把它留给操作系统来为您清理。

The code you've written uses a class that has a type of exception that must be handled. You can do that in one of two ways, by passing it up the chain i.e. re-throwing the exception, or by actually handling it as some of the other comments show by putting the close methods in a try catch of there own in the finally block.

Typically you will see these in file IO classes where you should always close the object or stream to ensure you leave it in a valid state. Otherwise you are leaving it up to the Operating System to clean up for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文