考虑到这一点的代码,我可以绝对肯定的是,finally块总是执行,不管something()是什么?
try {
something();
return success;
}
catch (Exception e) {
return failure;
}
finally {
System.out.println("I don't know if this will get printed out");
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的循环:
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Run Code Online (Sandbox Code Playgroud)
快速问题:是否return停止循环执行本身?
鉴于此代码:
String test() {
try {
return "1";
} finally {
return "2";
}
}
Run Code Online (Sandbox Code Playgroud)
语言规范是否定义了调用的返回值test()?换句话说:每个JVM中它总是一样的吗?
在Sun JVM中,返回值是2,但我想确定,这不依赖于VM.
我们可以在finally块中使用return语句.这会导致任何问题吗?
我不能确切地了解return工程try,catch.
try和finally没有catch,我可以把return里面try块.try,catch,finally,我不能忍受return的try块.catch块,我必须把return在外面try,catch,finally块.catch块throw Exception,我可以把return里面try块.它们如何正常工作?为什么我不能把它return放在try一块?
代码try,catch,finally
public int insertUser(UserBean user) {
int status = 0;
Connection myConn = null;
PreparedStatement myStmt = null;
try { …Run Code Online (Sandbox Code Playgroud) "finally"和"catch"之后写的有什么区别?
例如:
public boolean example() {
try {
// Code
} catch (RuntimeException e) {
// Code
} finally {
return true;
}
}
public boolean example() {
try {
// Code
} catch (RuntimeException e) {
// Code
}
return true;
}
Run Code Online (Sandbox Code Playgroud) 我遇到了这个问题,我无法理解它给出的输出的原因.
该计划是:
public static String method(){
String s = new String();
try
{
s = "return value from try block";
return s;
}
catch (Exception e)
{
s = s + "return value from catch block";
return s;
}
finally
{
s = s + "return value from finally block";
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
从try块返回值
现在,我调试它和值s在return s中陈述try阻滞return value from try block,return value from catch block后returned from finally block.
输出仍然是: …
类似的问题已被问到here。但这并没有提供答案。
try {
object = (Dev)Class.forName("Dev").newInstance();
} catch (Exception e)
{
throw new RuntimeException("Devis not available");
}
finally
{
return object;
}
Run Code Online (Sandbox Code Playgroud)
但finally块给出警告:
finally 块没有正常完成
但根据我的理解,finally块总是被执行并返回对象。为什么警告说它不会正常完成?
我有Java背景,最近开始学习Kotlin。现在我正在读一本书“Programming Kotlin”,我看到了一个带有 try-catch 表达式的片段。这促使我编写了一些简单的函数来比较它在 Java 和 Kotlin 中的工作方式。第一个功能:
fun tryExpExplicit(throwing: Boolean): Int {
return try {
if (throwing) {
throw RuntimeException()
}
return 1
} catch (e: Exception) {
return 2
} finally {
return 3
}
}
Run Code Online (Sandbox Code Playgroud)
按我的预期工作并且总是返回 3。
出乎意料的是,当我使用隐式返回时,行为不同
fun tryExpImplicit(throwing: Boolean): Int {
return try {
if (throwing) {
throw RuntimeException()
}
1
} catch (e: Exception) {
2
} finally {
3
}
}
Run Code Online (Sandbox Code Playgroud)
并且 3 永远不会返回。
为什么这两个函数的工作方式不同?
public boolean sendDeviceEvent() {
boolean status = false;
try {
device.sendEvent("blah...blah");
status = true;
} catch (Exception e) {
log.error("Failed to send NodeLowBattery Event - {} {}", createNodeLowBatteryNotification(), e.getCause());
} finally {
return status;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道上面的代码如何被认为是不好的做法,因为它从最后返回.根据字节码信息,最后不会突然返回,最后没有设置值.怎么会被认为是坏事?