关于 try-catch 语句中的 finally 的 Java 问题。

huangapple go评论75阅读模式
英文:

java question about try catch statement with finally

问题

这是由Java编写的代码。

public class work2{
    public static void main(String args[]) {
        try {
            int x = 0;
            for (x = 1; x < 4; x++);
            System.out.println(x);
        } catch(Exception e) {}
        finally {
            System.out.println("Error");
        }
    }
}

输出是:

4
Error

你能解释为什么输出是这样的吗?

英文:

Here is a code written by java.

public class work2{
public static void main(String args[]) {
    try {
        int x = 0;
        for (x=1; x&lt;4; x++); 
        System.out.println(x);
    } catch(Exception e) {}
    finally {
        System.out.println(&quot;Error&quot;);
    }
}

}

And the output is

> 4 Error

Would you explain why the output is looks like this?

答案1

得分: 1

始终记住在try-catch块中的一点:"无论是否有异常,finally都会执行"。现在来看看你的代码。在try块内,你有以下代码行:

 try {
    int x = 0;
    for (x=1; x<4; x++); 
    System.out.println(x);
}

这里的for循环以分号结束,这意味着它没有循环体。
现在,for循环将从i=1运行,直到i<4,然后以i的值4退出循环。

接下来的一行是一个打印语句,它打印i的当前值4。

之后,控制流进入finally块并打印"Error"。

这就是你获得输出的方式:

4
Error
英文:

Always remember in a try catch block - "finally always Run", irrespective of whether there is an exception or not. Now coming to your code. Inside the try block you have below line:

 try {
    int x = 0;
    for (x=1; x&lt;4; x++); 
    System.out.println(x);
}

Here the for loop ends in a semicolon, which means it doesn't have a body.
Now, the for loop will run from i=1 till i<4 and then will come out of loop with value of i as 4.

Now the next line is a print statement which prints the current value of i as 4.

After that the controls goes to the finally block and prints "Error"

That's how you get the output as

4
Error

答案2

得分: 0

以下是翻译好的部分:

`for` 循环您正在使用仅增加 x 的值但不将其打印出来因此您将 x 增加到 4然后将其打印出来最后部分无论如何都将被打印出来
如果您想要输出 x 的每个值您必须在 `for` 循环后面添加 `{}`。&lt;br&gt;
它会像这样

public class Main{
    public static void main(String args[]) {
        try {
            int x = 0;
            for (x=1; x&lt;4; x++) {
                System.out.println(x);
            }
        } catch(Exception e) {}
        finally {
            System.out.println("Error");
        }
    }
}
英文:

The for - Loop you're using is only increase the value of x but doesn't print it out. So you increase x to 4 and than print it out, the finally part will be printed anyway.
If you want to put out every value of x, you had to add { } after the for - Loop. <br>
It will look like:<br>

public class Main{
    public static void main(String args[]) {
        try {
            int x = 0;
            for (x=1; x&lt;4; x++) {
                System.out.println(x);
            }
        } catch(Exception e) {}
        finally {
            System.out.println(&quot;Error&quot;);
        }
    }
}

答案3

得分: -1

try {
    int x = 0;
    for (x=1; x<4; x++);
    System.out.println(x);
}

尝试块没有捕获任何异常,因此代码按原样运行。在 finally 块中:

finally {
    System.out.println("Error");
}

finally 块包含了所有必须执行的关键语句,无论是否发生异常。该块中的语句将始终执行,不管 try 块中是否发生异常,例如关闭连接、流等。

无论发生什么情况,它都会打印 "Error"。所以,Try 块打印了 4,然后 Finally 块打印了 "Error"。

英文:
try {
        int x = 0;
        for (x=1; x&lt;4; x++); 
        System.out.println(x);
    }

the try block didn't catch any exception, so the code were run as it is.
and in

finally {
        System.out.println(&quot;Error&quot;);
    }

> A finally block contains all the crucial statements that must be
> executed whether exception occurs or not. The statements present in
> this block will always execute regardless of whether exception occurs
> in try block or not such as closing a connection, stream etc.

it will print "Error" regardless what happen.
So, Try block printed 4, and then Finally block print "Error"

答案4

得分: -1

你在 try 块中编写导致异常的语句。这些异常会在 catch 块中得到处理。即使没有触发异常,finally 块也会被执行。

在你的问题中,初始时 x=0;。由于在 for 循环后有一个分号 ;,循环将被执行 3 次,当 x=4 时,条件不满足,并打印值为 4 的 x。然后会执行 finally 块。

英文:

You write the statements which cause exception in try block. And these exceptions are handled using catch block. finally block is executed even if exception is not triggered.

In your question, x=0; initially. Since there is a semicolon ; after for loop, the loop will be executed 3 times and when x=4, the condition fails and it prints the value of x that is 4. And finally block is executed.

答案5

得分: -1

public class Main{
    public static void main(String args[]) {
        try {
            int x = 0;
            for (x = 1; x < 4; x++)
                System.out.println(x);
        } catch (Exception e) {
        }
        finally {
            System.out.println("Error");
        }
    }
}

输出应为:
1
2
3
错误

你应该移除 for 循环后面的分号。

finally 块包含了所有必须执行的关键语句,无论是否发生异常。... 此块中的语句将始终执行,无论 try 块中是否发生异常,比如关闭连接、流等。

英文:
    public class Main{
public static void main(String args[]) {
    try {
        int x = 0;
        for (x=1; x&lt;4; x++)
        System.out.println(x);
    } catch(Exception e) {}
    finally {
        System.out.println(&quot;Error&quot;);
    }
}
}

The output should be
1
2
3
error

you should remove the ; after for loop .

A finally block contains all the crucial statements that must be executed whether exception occurs or not. ... The statements present in this block will always execute regardless of whether exception occurs in try block or not such as closing a connection, stream etc.

huangapple
  • 本文由 发表于 2020年10月22日 13:30:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64475842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定