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

Discussion

Which line rests on a mistaken assumption?

public class Sample16
{
    public static void main(String[] args)
    {
        try
        {
            assert false : "broken";
        }
        catch (Exception e)
        {
            System.out.println("recovered");
        }
    }
}

  • A.assert false : "broken";
  • B.System.out.println("recovered");
  • C.Nothing here is mistaken.
  • D.catch (Exception e)

Answer: D

AssertionError extends Error, not Exception, so this catch clause never matches and the error leaves main unhandled. The class was placed under Error deliberately, to discourage recovering from a failed assertion. Catching Throwable or AssertionError by name would match, though neither is advisable.

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

Post your comment

Please log in or register to post a comment.