Should other code be added in the dosomething() method for it to compile and execute?

An unchecked exception occurs in a method dosomething(). Should other code be added in the dosomething() method for it to compile and execute?

An unchecked exception occurs in a method dosomething(). Should other code be added in the dosomething() method for it to compile and execute?

A.
The Exception must be caught

B.
The Exception must be declared to be thrown.

C.
The Exception must be caught or declared to be thrown.

D.
No other code needs to be added.

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.
Thus, Java compilers do not require that you declare or catch unchecked exceptions in your program code.



Leave a Reply 7

Your email address will not be published. Required fields are marked *


Antonello

Antonello

Agree with C

Antonello

Antonello

Sorry answer is D

James

James

Answer is D.

According to Oracle, the Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses).

Kalil Peixoto

Kalil Peixoto

I agree on letter D

Jessey

Jessey

A because it needs compile and execute, instead of compile only (D).