考虑到这一点的代码,我可以绝对肯定的是,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) 我想知道为什么Java编译器会接受以下代码:
public class Main {
public static void main(String ... args){
System.out.println("a() = " + a());
}
public static String a (){
try {
return "a";
}catch(Throwable t){
}finally{
return "b";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可以也不应该奏效.java规范声明finally将始终执行块,但同时已指定返回值.所以要么你不能执行该return "b"语句,因为你已经退出return "a",这是不正确的.
但是,另一个选择是执行该return "b"语句,从而完全忽略该return "a"语句......
我会说两者都是错的,我希望这不会编译.然而,它编译并运行良好.我将把答案作为一个很好的练习留给读者;).
基本上我的问题是:除了不好的做法,这会被认为是一个Java错误,还是除了混淆之外还有其他奇妙的用途?
问题不是如果它是一个bug,已经得到了回答,但它有不错的用例吗?
public class Test2 {
public static void main(String[] args) {
Test2 obj=new Test2();
String a=obj.go();
System.out.print(a);
}
public String go() {
String q="hii";
try {
return q;
}
finally {
q="hello";
System.out.println("finally value of q is "+q);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么hii从函数返回后打印go(),在finally块中值已更改为"hello"?
该计划的输出是
finally value of q is hello
hii
Run Code Online (Sandbox Code Playgroud) 我运行这段代码:
public static void main(String[] args) {
System.out.println(catcher());
}
private static int catcher() {
try {
System.out.println("TRY");
thrower();
return 1;
} catch (Exception e) {
System.out.println("CATCH");
return 2;
} finally {
System.out.println("FINALLY");
return 3;
}
}
private static void thrower() {
throw new RuntimeException();
}
Run Code Online (Sandbox Code Playgroud)
我希望在输出中看到这个:
TRY
CATCH
FINALLY
2
Run Code Online (Sandbox Code Playgroud)
但令人惊讶的是输出是:
TRY
CATCH
FINALLY
3
Run Code Online (Sandbox Code Playgroud)
我糊涂了.return 2声明在哪里?这是return at finally一种不好的做法吗?
这并不能完全回答我的问题,也可能不相关,因为它是 java 而不是 javascript:try、catch 和 finally 中的执行顺序是什么
我知道将 return 放入finally块中将优先于catch块中的 return ,但是序列事件如何与以下代码一起使用?
function confusion(){
var test = "a";
try{
return test;
}
finally{
test = "b";
}
}
console.log("Result", confusion()); //"a"
Run Code Online (Sandbox Code Playgroud)
我对返回任何东西的一般理解是控制通常返回到调用方法的上下文,因此继续执行的事实让我感到困惑。
在这种情况下,我认为它可能会像这样:
- 函数设置为返回test->finally更改值test->test返回更新后的值。
但相反,它似乎为该try块保留了某种状态并finally错过了时间?
public class Abc {
public static void main(String args[]) {
System.out.println(Abc.method());
}
static int method() {
try {
throw new Exception();
}
catch(Exception e) {
throw new Exception();
}
finally {
return 4;
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么返回值为4?
在Java中使用异常处理时,我注意到在Java中的catch块中执行某些非法运行时操作时不会抛出异常.
这是语言中的错误还是我错过了什么?有人可以调查一下 - 因为为什么没有从catch块中抛出异常.
public class DivideDemo {
@SuppressWarnings("finally")
public static int divide(int a, int b){
try{
a = a/b;
}
catch(ArithmeticException e){
System.out.println("Recomputing value");
/* excepting an exception in the code below*/
b=0;
a = a/b;
System.out.println(a);
}
finally{
System.out.println("hi");
return a;
}
}
public static void main(String[] args) {
System.out.println("Dividing two nos");
System.out.println(divide(100,0));
}
Run Code Online (Sandbox Code Playgroud)
}
我有一个班级-公共班级Check {
public static void main(String[] args) {
System.out.println(new Check().isRight());
}
public boolean isRight(){
try{
return true;
}finally{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
isRight方法在finally子句中返回true和false,并且由于将肯定调用finally子句,因此最终它返回false,所以try中的数据实际发生了什么。我需要知道输出背后的逻辑原因,但我无法正确理解。