我是 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();
}
}