What must a method declare if it can throw a checked exception it doesn't catch?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

What must a method declare if it can throw a checked exception it doesn't catch?

Explanation:
In languages with checked exceptions, a method that might propagate a checked exception must declare it in its signature so callers are forced to handle or re-declare it. This is done with a throws clause listing the exception type(s) that can escape the method. Since the scenario specifies the method does not catch the exception, it must declare it so the caller knows to handle it. An example is a method signature like: public void read() throws IOException { … }. The keyword throws is used in the signature to indicate the potential exception. The other options don’t fit: catch is used inside the method body to handle exceptions, not in the signature; final isn’t used to declare exceptions; and throw is used inside the method to actually throw an exception, not to declare it in the signature.

In languages with checked exceptions, a method that might propagate a checked exception must declare it in its signature so callers are forced to handle or re-declare it. This is done with a throws clause listing the exception type(s) that can escape the method. Since the scenario specifies the method does not catch the exception, it must declare it so the caller knows to handle it. An example is a method signature like: public void read() throws IOException { … }. The keyword throws is used in the signature to indicate the potential exception.

The other options don’t fit: catch is used inside the method body to handle exceptions, not in the signature; final isn’t used to declare exceptions; and throw is used inside the method to actually throw an exception, not to declare it in the signature.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy