1
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?

  • Checked exceptions must be caught. Runtime exceptions should be prevented. The former represent exceptional conditions that occur extrinsically, outside of program control, like a file closing prematurely. The latter represent programmer f(oul)ups. Programmer f(oul)ups should not be abided. – Lew Bloch May 17 '17 at 17:58

3 Answers3

1

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
Community
  • 1
  • 1
syntagma
  • 23,346
  • 16
  • 78
  • 134
0

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.

Brian Ecker
  • 2,017
  • 1
  • 21
  • 30
  • I understood the same and thats what I want to say that ts written that Rintime xceptions need never be handled or declared.Yes it is true that they need not be declared but we handle them then how can we say that they need never be handled? – Ankit Srivastava May 17 '17 at 17:40
  • "handled" in this context means caught in a `catch` block. – Brian Ecker May 17 '17 at 17:48
0

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.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34