How does return work in try, catch, finally in Java?(在 Java 中,try、catch 和 finally 中的 return 是如何工作的?)
问题描述
我无法准确理解 return 在 try、catch 中的工作原理.
I can't understand exactly how return works in try, catch.
- 如果我有
try和finally而没有catch,我可以将return放入try块. - 如果我有
try、catch、finally,我不能把return放在尝试块. - 如果我有一个
catch块,我必须将return放在try、catch之外,finally块. - 如果我删除
catch块和throw Exception,我可以将return放在try块内.
- If I have
tryandfinallywithoutcatch, I can putreturninside thetryblock. - If I have
try,catch,finally, I can't putreturnin thetryblock. - If I have a
catchblock, I must put thereturnoutside of thetry,catch,finallyblocks. - If I delete the
catchblock andthrow Exception, I can put thereturninside thetryblock.
它们究竟是如何工作的?为什么我不能将 return 放在 try 块中?
How do they work exactly? Why I can't put the return in the try block?
带有try、catch、finally
public int insertUser(UserBean user) {
int status = 0;
Connection myConn = null;
PreparedStatement myStmt = null;
try {
// Get database connection
myConn = dataSource.getConnection();
// Create SQL query for insert
String sql = "INSERT INTO user "
+ "(user_name, name, password) "
+ "VALUES (?, ?, ?)";
myStmt = myConn.prepareStatement(sql);
// Set the parameter values for the student
myStmt.setString(1, user.getUsername());
myStmt.setString(2, user.getName());
myStmt.setString(3, user.getPassword());
// Execute SQL insert
myStmt.execute();
} catch (Exception exc) {
System.out.println(exc);
} finally {
// Clean up JDBC objects
close(myConn, myStmt, null);
}
return status;
}
带有 try 的代码,finally 没有 catch
Code with try, finally without catch
public int insertUser(UserBean user) throws Exception {
int status = 0;
Connection myConn = null;
PreparedStatement myStmt = null;
try {
// Get database connection
myConn = dataSource.getConnection();
// Create SQL query for insert
String sql = "INSERT INTO user "
+ "(user_name, name, password) "
+ "VALUES (?, ?, ?)";
myStmt = myConn.prepareStatement(sql);
// Set the parameter values for the student
myStmt.setString(1, user.getUsername());
myStmt.setString(2, user.getName());
myStmt.setString(3, user.getPassword());
// Execute SQL insert
myStmt.execute();
return status;
} finally {
// Clean up JDBC objects
close(myConn, myStmt, null);
}
}
推荐答案
是的,很混乱.
在Java中,非void函数的所有程序控制路径必须以return结束,或抛出异常.这条规则说得很好,很简单.
In Java, all program control paths of a non-void function must finish with a return, or throw an exception. That's the rule put nice and simply.
但是,令人憎恶的是,Java 允许您将 extra return 放在 finally 块中,这会覆盖之前遇到的任何 返回:
But, in an abomination, Java allows you to put an extra return in a finally block, which overrides any previously encountered return:
try {
return foo; // This is evaluated...
} finally {
return bar; // ...and so is this one, and the previous `return` is discarded
}
这篇关于在 Java 中,try、catch 和 finally 中的 return 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中,try、catch 和 finally 中的 return 是如何工作的?
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
