2

我是 Java 语言的新手,无法理解该程序中 finally 块的行为。该程序在打印 BC 后应该退出,而它正在打印 BCD。请帮忙。

class Main 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A"); 
        }  
        catch (Exception ex) 
        {
            System.out.print("B");  
        } 
        finally 
        {
            System.out.print("C"); 
        } 
        System.out.print("D"); 
    }  
    public static void badMethod() throws Exception
    {
        throw new Exception(); /* Line 22 */
    } 
}
4

10 回答 10

8

您正在捕获异常(在 catch 块中)而不是重新抛出它 - 因此您正在有效地处理异常,然后执行就好像它没有被抛出一样继续进行。程序只退出是因为它到达了main方法的结尾——它不像是突然终止。

If you change your code to either rethrow the exception from the catch block or just don't catch it in the first place (both of which will require you to declare that main throws Exception, of course) then it won't print D.

于 2012-04-17T13:59:17.130 回答
4

There is nothing to cause the program to exit.

The whole point of a catch block is to catch the exception and prevent it from propagating further.
After catching an exception, execution continues.

于 2012-04-17T13:59:28.963 回答
4

The finally block doesn't finalize the program, it just ensure to execute every time the try catch block runs whereas there is an exception or not..

于 2012-04-17T14:01:01.303 回答
1

如果您在块BC中重新抛出异常,它将打印。catch

catch (Exception ex) 
{
    System.out.print("B");  
    throw ex;
}

然后你必须将你的主要声明为

public static void main(String [] args) throws Exception
于 2012-04-17T13:58:50.780 回答
1

Finally is called at the end of a try/catch block. It gets called even if the try fails and the catch is executed. The Finallyblock itself is only not executed if the program is killed somehow (JVM dies, forced to close, etc.)

In your example D is executing because it is outside of the try/catch/finally{} blocks.

There is a nice blog post on catching exceptions and the try/catch/finally behaviour here.

于 2012-04-17T14:00:07.830 回答
1

Which is correct. The above code will.

  1. Try to execute badMethod and will fail
  2. Execute the code in catch
  3. Execute the code in finally
  4. Continue in execution - print out the D
于 2012-04-17T14:00:21.137 回答
1

The finally block is processed after the try or catch block runs (depending on whether an exception was thrown/caught or not). If the exceptions were all caught properly and handles by the catch, it will run finally and then continue running the rest of the method.

于 2012-04-17T14:00:49.057 回答
1

Your program is functioning in the following way: 1. Call a bad method that throws an exception 2. Catch the exception 3. Execute the finally block 4. Resume

In Java, when an exception gets thrown it does not necessarily end program execution if the exception is handled. Your program does not actually handle the exception but catches it anyway and this is enough for the JVM to think it's ok to resume execution.

The output BCD is quite the right output.

于 2012-04-17T14:02:20.247 回答
1

Finally is the must executable block of java program. It allows all the allocated resources of the currently running program to get free and make it available for the other applications if required.

This is mainly used when we share the common resources like Database or the devices.

于 2012-04-17T14:10:07.643 回答
1

Think of the finally block as an independent block of code(s) you'll still expect your method to continue executing irrespective of whether an exception occurs or not.

And so in your case, badMethod throws an exception which is consequently caught by the catch block, your main then continue by executing the finally block independently.

In other words, if badMethod decides not to throw an exception, your finally block would still continue execute before reaching the end of the method.

Therefore with finally been an independent block it is then possible to also do something like this in your main code if prefered.

try
{
 fooA();
}
finally
{
 fooB();
}
于 2012-04-17T14:14:00.810 回答