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

Discussion

Which line contains the error?

public class Sample68
{
    static int count()
    {
        try
        {
            assert false : "unreachable";
            return 1;
        }
        finally
        {
            return 2;
        }
    }

    public static void main(String[] args)
    {
        System.out.println(count());
    }
}

  • A.assert false : "unreachable";
  • B.System.out.println(count());
  • C.The code is correct as it stands.
  • D.return 2;

Answer: D

A return inside finally discards whatever the try was doing, including a thrown AssertionError, so the method quietly returns 2 and the failed assertion vanishes. Returning from finally swallows errors and should never be done; the assertion is only the victim here.

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

Post your comment

Please log in or register to post a comment.