Ankit Srivastava
Ankit Srivastava

Reputation: 75

Do runtime exception have to be handled?

Which statements are true?

<a>All classes of Exception extends Error.
<b>All classes of Error extends Exception.
<c>All Errors must be handled or declared.
<d>All classes of Exception extends Throwable.
<e>All Throwables must be handled or declared.
<f>All Exceptions must be handled or declared.
<g>Runtime Exceptions need never be handled or declared.

According to me answer should be d and f as I think runtime exceptions such as Arithmetic Exceptions need to be handled as we always put them in try and catch block so handling it but in ocjp book by "Kathy Sierra" it is given that answer is d and g. Who is correct? Have I misunderstood something?

Upvotes: 1

Views: 2524

Answers (3)

dumbPotato21
dumbPotato21

Reputation: 5695

I think runtime exceptions such as Arithmetic Exceptions need to be handled as we always put them in try and catch block

No, not really.I mean, you can put them in a try...catch, but it is not always. For example,

int i = 2;
int j = i/0;

We don't need a try..catch for this. You could do

int i = 2;
int j;
try{
    j = i/0;
}catch(Exception e){
    System.out.println("Divide By Zero");
}

but its not necessary. On the other hand, Checked Exception need always need a try...catch. But, since Exceptions are both checked and unchecked this statement is false.

However, the last is true, since runtime exceptions don't need a try...catch. Again you could, but its not necessary.

Upvotes: 0

syntagma
syntagma

Reputation: 24294

a. All classes of Exception extends Error.

b. All classes of Error extends Exception.

d. All classes of Exception extends Throwable.

Both Exception and Error extend Throwable, so (a) and (b) are false and (d) is true.

c. All Errors must be handled or declared.

e. All Throwables must be handled or declared.

f. All Exceptions must be handled or declared.

All above are falsem, reed more about it here: Java: checked vs unchecked exception explanation

g. Runtime Exceptions need never be handled or declared.

True, same reason as in the link above.

Here is a simple example of an unhandled ArithmeticException (which extends RuntimeException which extends Exception):

int a = 42 / 0; // that would only throw at runtime, no need to try..catch it

Upvotes: 1

Brian Ecker
Brian Ecker

Reputation: 2077

I think perhaps you're misunderstanding what is meant by "handled." Although it might be sensible to catch and do something with an exception like an ArithmeticException, which is a RuntimeException, you don't actually have to catch it. When the author says "All Exceptions must be handled or declared," handling means catching. You do not have to catch or declare a RuntimeException. Declaring in this case means adding throws YourRuntimeException to your method.

In your particular example, you do not have to always catch an ArithmeticException or declare throws ArithmeticException because it is a RuntimeException.

Upvotes: 0

Related Questions