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?

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:
Because the Java programming language does not require methods to catch or to
specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may
be tempted to write code that throws only unchecked exceptions or to make all their exception
subclasses inherit from RuntimeException. Both of these shortcuts allow programmers to write
code without bothering with compiler errors and without bothering to specify or to catch any
exceptions. Although this may seem convenient to the programmer, it sidesteps the intent of the
catch or specify requirement and can cause problems for others using your classes.



Leave a Reply 2

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


Noname

Noname

Generally an unchecked exceptions (i.e. RuntimeException or Error instances) may stay unprocessed as they are not checked at the compile time and will not cause compilation fail. Note I use term “may” not “must”.
Am I right?

Kurt

Kurt

The correct answer is D