【发布时间】:2010-10-15 05:21:16
【问题描述】:
我在 NetBeans 中收到一个错误,提示我必须在此方法中抛出 SQLException:
private void displayCustomerInfo(java.awt.event.ActionEvent evt)
{
int custID = Integer.parseInt(customerID.getText());
String info = getCustomerInfo(custID);
results.setText(info);
}
此方法由 NetBeans 创建,因此不允许我编辑签名并引发异常。这就是我创建getCustomerInfo() 方法的原因。此方法确实会引发异常,因为它使用一种方法从数据库中检索有关客户的信息。
public String getCustomerInfo(int cid) throws SQLException
{
Customer c = proc.getCustomer(cid);
// get info
return "info";
}
getCustomer 方法也抛出异常,proc.java 编译。
确切的错误是
unreported exception java.sql.SQLException; must be caught or declared to be thrown
【问题讨论】:
-
原谅我,但为什么每个人都只是在这里谈论 try-catch?这是一种选择,但不是唯一的选择。为什么不能抛出异常?为什么netbeans不允许它?我不使用 Netbeans(我使用 eclipse),但 IDE 不允许更改生成的代码听起来很愚蠢。