Java - try-catch 中的变量范围 - 大多数答案与官方 Java 教程之间的对比
Java - Variable scope inside a try-catch - Contrast between most answers and an official Java tutorial
我正在阅读以下内容 official guide 我发现了一个问题,将这两个代码片段一起使用会导致错误(stmt 对象没有作用域):
处理结果集对象
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
}
正在关闭连接
} finally {
if (stmt != null) { stmt.close(); }
}
如果我尝试在 finally 块中 stmt.close()
我会得到一个错误,因为他的范围内没有 stmt 变量,那是因为(据我所知)的实际范围stmt 对象在 try 块中。
我的问题很简单,这两段代码能协同工作吗?
我找到的答案是否定的,只有将 stmt 对象的实例化移到 try 块之外才能生成工作代码段。
有人可以给我他的想法吗?
我只是想了解该论点是否有某些方面我还不清楚。
非常感谢任何愿意帮助我的人。
据推测,您已在 try
内部而不是外部声明了 stmt
对象。代码应如下所示:
Statement stmt;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} finally {
if (stmt != null) { stmt.close(); }
}
但是,您可以使用 Statement
实现 AutoClosable
的事实,并通过使用 try with resources
(Java 中添加的 Java 功能实际上省略了 finally 块=] 6 iirc).看起来像:
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} catch (SQLException e) {
// Log it
}
您可以在此处阅读有关 AutoClosable
和 try with resources
的更多信息:
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
为了明确这一点,您应该将 stmt 分配给 null,以便它存在并被初始化:
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} finally {
if (stmt != null) { stmt.close(); }
}
我正在阅读以下内容 official guide 我发现了一个问题,将这两个代码片段一起使用会导致错误(stmt 对象没有作用域):
处理结果集对象
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
}
正在关闭连接
} finally {
if (stmt != null) { stmt.close(); }
}
如果我尝试在 finally 块中 stmt.close()
我会得到一个错误,因为他的范围内没有 stmt 变量,那是因为(据我所知)的实际范围stmt 对象在 try 块中。
我的问题很简单,这两段代码能协同工作吗?
我找到的答案是否定的,只有将 stmt 对象的实例化移到 try 块之外才能生成工作代码段。
有人可以给我他的想法吗?
我只是想了解该论点是否有某些方面我还不清楚。
非常感谢任何愿意帮助我的人。
据推测,您已在 try
内部而不是外部声明了 stmt
对象。代码应如下所示:
Statement stmt;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} finally {
if (stmt != null) { stmt.close(); }
}
但是,您可以使用 Statement
实现 AutoClosable
的事实,并通过使用 try with resources
(Java 中添加的 Java 功能实际上省略了 finally 块=] 6 iirc).看起来像:
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} catch (SQLException e) {
// Log it
}
您可以在此处阅读有关 AutoClosable
和 try with resources
的更多信息:
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
为了明确这一点,您应该将 stmt 分配给 null,以便它存在并被初始化:
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} finally {
if (stmt != null) { stmt.close(); }
}