我有这样的功能:
private void formWindowOpened(java.awt.event.WindowEvent evt) {
Connection conn = null; // connection object
Statement stmt = null; // statement object
ResultSet rs = null; // result set object
DatabaseMetaData metadata = null;
try {
conn = getConnection(); // without Connection, cannot do much
metadata = conn.getMetaData();
rs = metadata.getTables(null, null, "%", null);
if (checkDefaultTables(rs)) {
lStatus.setText("Message: Please insert 2 tables recommend");
}
} catch(ClassNotFoundException ce) {
// if the driver class not found, then we will be here
System.out.println(ce.getMessage());
} catch(SQLException e) {
// something went wrong, we are handling the exception here
if (conn != null) {
conn.rollback();
conn.setAutoCommit(true);
}
System.out.println("--- SQLException Caught ---");
// iterate and get all of the errors as much as possible
while (e != null) {
System.out.println("Message: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("ErrorCode: " + e.getErrorCode());
System.out.println("---");
e = e.getNextException();
}
} finally {
// close the db resources
try {
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
//System.out.println("Exception: " + e.getMessage());
}
}
}
我遇到错误,行conn = getConnection()
,conn.rollback()
和conn.setAutoCommit(true)
需要例外。我尝试在throws SQLException, Exception
之后添加private void formWindowOpened(java.awt.event.WindowEvent evt)
,但我无法编辑此行。
如何为这种情况添加例外?
P / S:我正在使用NetBean IDE而且我是新手。答案 0 :(得分:0)
为什么不能编辑签名?
它是来自超级班吗?你有权使用它吗?您应该咨询API的作者,以便在那里包含您的需求。没有选项来扩展子类中方法的异常列表(尽管可以省略)。
OTOH,您可以抛出非托管异常( RuntimeException 的子类)。它们既不需要声明也不必处理(例如 NumberFormatException , IndexOutOfBoundsException )。
查看官方Java教程的Exceptions chapter,以澄清该语言的基本概念。