nerdexam
Oracle

1Z0-803 · Question #16

A method doSomething () that has no exception handling code is modified to trail a method that throws a checked exception. Which two modifications, made independently, will allow the program to compil

The correct answer is A. Catch the exception in the method doSomething(). B. Declare the exception to be thrown in the doSomething() method signature.. Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following: A try statement that catches the exception. The try must provide a handler for the exception, as

Handling Exceptions

Question

A method doSomething () that has no exception handling code is modified to trail a method that throws a checked exception. Which two modifications, made independently, will allow the program to compile?

Options

  • ACatch the exception in the method doSomething().
  • BDeclare the exception to be thrown in the doSomething() method signature.
  • CCast the exception to a RunTimeException in the doSomething() method.
  • DCatch the exception in the method that calls doSomething().

How the community answered

(17 responses)
  • A
    71% (12)
  • C
    12% (2)
  • D
    18% (3)

Explanation

Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following: * A try statement that catches the exception. The try must provide a handler for the exception, as described in Catching and Handling Exceptions. * A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception, as described in Specifying the Exceptions Thrown by a Method. Code that fails to honor the Catch or Specify Requirement will not compile.

Topics

#checked exceptions#exception handling#throws keyword#try-catch block

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice