Machine-translated by Google. Questions are authored in English.
Top

Discussion

The program is run with java -ea Listing17 - what will be the output?

public class Listing17
{
    public static void main(String[] args)
    {
        try
        {
            assert false;
        }
        finally
        {
            System.out.println("finally");
        }
    }
}

  • A.finally, then the AssertionError propagates.
  • B.The AssertionError propagates; finally does not run.
  • C.The program prints finally only
  • D.Compilation fails.

Answer: A

A finally block runs whether the try completes normally or is left by a throw, and AssertionError is no exception to that rule. So finally is printed first, and the error then continues to propagate out of main because nothing caught it. finally does not swallow the error.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.