Java中try-catch-finally抛出异常问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为
close()
方法:可能会抛出
SQLException
并且您没有将它们封装在 try/catch 块中。方法很可能从finally 子句中抛出异常,并且如果没有catch 子句处理这些异常,则必须声明该方法来抛出这些异常。
基本上,你需要做类似的事情
It's because the
close()
methods: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
将
dbConn = getConnection();
放入try
部分。 它也可以抛出SQLException
。Put
dbConn = getConnection();
in thetry
section. It can throw anSQLException
too.除了编译器错误之外,此代码在运行时也会失败。
当您关闭创建
ResultSet
的Connection
时,ResultSet
将失效,并且对其进行的任何调用都将失败。 (当然,需要实现一些奇怪的 JDBC 驱动程序才能工作,但这是 JDBC API 的错误用法。)ResultSet
的范围和生命周期不能比其父级更宽代码>连接
和语句
。Aside from the compiler errors, this code will fail at runtime.
When you close the
Connection
from which theResultSet
is created, theResultSet
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 parentConnection
andStatement
.您编写的代码使用的类具有必须处理的异常类型。您可以通过两种方式之一来做到这一点,将其传递到链上,即重新抛出异常,或者通过将 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.