Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam question regarding exceptions

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question was obtained from a Whizlab's exam.
I emailed the author my concerns with respect to this question and did not receive a response.

----- ----- ----- -----
Which statement is true about checked exceptions?


A. Checked exceptions are only those classes which are subclasses of java.lang.Exception and which do not inherit from java.lang.RuntimeException

B. Checked exceptions are those exceptions which directly or indirectly inherit from java.lang.RuntimeException

C. Checked exceptions are only those which are subclasses of java.lang.Throwable and which do not inherit from java.lang.RuntimeException or java.lang.Error

D. Checked exceptions are only those exceptions which directly inherit from java.lang.Exception and which are not subclasses of java.lang.RuntimeException

Answer : C
Correct answer is option C.
Any subclass of java.lang.Throwable which does not inherit from java.lang.RuntimeException or java.lang.Error is a checked exception.
#
______________________________________________________________________________________________________________________

------
Based on the diagram I have attached,




options A and D would also be correct.

Option B) is obviously incorrect.

Question:
Is option C) more specific or accurate than options A or D,
OR
should the question have read:

Which of the following 3 options below are correct?



 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation given by answer C is correct: Any subclass of java.lang.Throwable which does not inherit from java.lang.RuntimeException or java.lang.Error is a checked exception.

The reason why A and D are not correct, is because they focus on subclasses from class java.lang.Exception and have the word "only" in the statement (which is incorrect based on the above explanation).
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for the reply.
I am assuming that this definition
(*)
Any subclass of java.lang.Throwable which does not inherit from java.lang.RuntimeException or java.lang.Error is a checked exception

originally came from the Sun website and is now on the Oracle website.

Oracle discusses checked exceptions here:
http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html

I was interested in knowing if the exact wording in the test question (*)
came from Oracle's website.

Thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think that this "definition" came from Sun/Oracle website?

When the different types of exceptions are introduced, it's mostly donelike this: "Runtime exceptions (+ subclasses) and errors (+ subclasses) are introduced. All other exceptions are checked exceptions". And I can find a somewhat similar "definition" on Oracle's website (on the link you provided): All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With regards to the way RuntimeExceptions are introduced,

Runtime exceptions (+ subclasses)


do you mean for example in the case of ArrayIndexOutOfBoundsException

Class ArrayIndexOutOfBoundsException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException

that the RuntimeException parent class is introduced followed by its subclasses,
and we need to conceptually differentiate them from "checked exceptions?"


 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checked Exceptions are subclasses of java.lang.Exception. Now any "All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses." this means the same things... Checked Exceptions are subclasses of java.Lang.Exception. Basically that means there cannot be any checked exception which is directly a subclass of Throwable, it will always extend Exception. Isn't it? At least that is what I got from the two conditions... Anyway if these following are true for Checked exceptions then I don't see how come A and D are wrong.. If you remove "only" from the options then A and D are wrong...

1. subclasses of java.lang.Exception
2. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aki,

If you look at the diagram from Edward Finegan (attached to the question) as I did when I took the test,
you can draw the conclusion that A and D are correct.

Initially I thought A and D could be correct based on the diagram from Edward Finegan.

I will accept Roel De Nijs explanation and go with the answer C.
Roel gave a convincing argument as to why answer C is correct.
Whizlabs never got back to me on this question.


 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:The reason why A and D are not correct, is because they focus on subclasses from class java.lang.Exception and have the word "only" in the statement (which is incorrect based on the above explanation).


This is confusing, Checked Exceptions are subclasses of Exceptions? Isn't it? Checked Exceptions are subclasses of Exceptions(which is a subclass of Throwable) then A and D are also right choices. Don't tell me that Oracle hasn't mentioned that Checked Exceptions are subclasses of Exceptions instead they said any exception which is not a Runtime or Error or its subclasses are Checked Exceptions. Please correct me if I'm wrong but I can't buy the idea that Checked Exceptions are subclasses of Throwable but not subclasses of Exceptions (always extends Exception hehe) I don't know if I'm trying to explain my understanding.


Why option A is correct - It says subclass of Exception(Oh it can be Runtime but Error is definitely ruled out) but they explicitly then say it that it doesn't inherit Runtime.
Why option D is correct - It says "directly inherit" from Exceptions(This is true) and when it says only and directly then every other option is ruled out cause Runtime is a subclass and Error do not extend Exception.

I hope I make myself clear with my understanding. Please let me know if I'm misunderstanding something here.

Thanks,
Aakash
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:they said any exception which is not a Runtime or Error or its subclasses are Checked Exceptions.

So that clearly means if your custom exception extends Throwable you have a checked exception (because it's not a RuntimeException nor an Error).

This won't compile:


Aki Mohan wrote:Please correct me if I'm wrong but I can't buy the idea that Checked Exceptions are subclasses of Throwable but not subclasses of Exceptions

I never said that checked exceptions are only subclasses of Throwable and not of Exception. I stated that statement A is incorrect, because it states that only those classes which are subclasses of Exception (and not of RuntimeException) are checked exception. That's not true, because MyThrowable (which doesn't extend Exception, but Throwable) is a checked exception. For the same reason D is false. I can create an example of a checked exception (MyThrowable) which doesn't comply to both statements (my class is not a subclass from Exception), I have proven that both statements are false (because of the word "only"). QED
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Aki Mohan wrote:they said any exception which is not a Runtime or Error or its subclasses are Checked Exceptions.

So that clearly means if your custom exception extends Throwable you have a checked exception (because it's not a RuntimeException nor an Error).

This won't compile:


Even if it says that any class which is not a Runtime/Error or their subclasses is a CheckedException but still "CheckedException extends Exception". That means Checked Exception is a subclass of Exception(which means it can never be related to Error or Runtime). Options A and D are still correct. Isn't this weird that Checked Exception has to be a subclass of Exception and still it is said that any exception which is not a Runtime/Error or their subclasses is a CheckedException???
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:----> This won't work because of polymorphism since CheckedException already extends exception.

I don't know why you mention CheckedException (my class is named MyThrowable). I have no clue at all what polymorphism has to do with it

If you have made the effort to try the code snippet in any IDE (or using any text editor and javac) you would have noticed that it doesn't compile on the 4th line of that code snippet (throw new MyThrowable();), the 1st line compiles just fine. Reason why compilation fails on the 4th line: Unhandled exception type MyThrowable (because MyThrowable is a checked exception and thus should be handled or declared)


Aki Mohan wrote:Options A and D are still correct. Isn't this weird that Checked Exception has to be a subclass of Exception and still it is said that any exception which is not a Runtime/Error or their subclasses is a CheckedException???

I can't do more than show you an example of a checked exception (namely MyThrowable) which is different from the statements A and D. The MyThrowable class clearly proves statements A and D are wrong, because according to these statements checked exceptions can only (and I can't stress that word enough) be subclasses of java.lang.Exception. My example shows that any subclass of Throwable is also a checked exception (which makes both statements incorrect).

If statement A would have been Subclasses of java.lang.Exception which do not inherit from java.lang.RuntimeException are checked exceptions. statement A would be true. But the original statement A is false!
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Roel,

Yeah I understand your point and I agree to it. Well theoretically both options A and D are wrong but again MyThrowable extends Throwable and MyThrowable extends Exception should also be wrong, polymorphism. Isn't it? Let me know if I'm missing any point here.

Thanks,
Aakash
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:but again MyThrowable extends Throwable and MyThrowable extends Exception should also be wrong, polymorphism. Isn't it?

Huh?
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Aki Mohan wrote:but again MyThrowable extends Throwable and MyThrowable extends Exception should also be wrong, polymorphism. Isn't it?

Huh?


umm, I'm so confused now hehe.

B extends A

C extends B

Then we can't right C extends A, right? As C can extend only one class but can implement many other classes.

A- Throwable
B - Exception
C - MyThrowable
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:Then we can't right C extends A, right? As C can extend only one class but can implement many other classes.

A few remarks:
  • If B extends A and C extends B, then C definitely extends A. C only extends 1 class (namely B). That rule has nothing to do with the superclass, otherwise you would not be able to use inheritance (because each class already extends Object).
  • Something like class C extends A, B is not allowed because a class can only extend one class (no multiple inheritance allowed).
  • A class can implement interfaces, not classes. But indeed classes can implement multiple interfaces.


  • I still don't understand how this is related to the above problem.
     
    Aki Mohan
    Ranch Hand
    Posts: 99
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Aki Mohan wrote:Then we can't right C extends A, right? As C can extend only one class but can implement many other classes.

    A few remarks:
  • If B extends A and C extends B, then C definitely extends A. C only extends 1 class (namely B). That rule has nothing to do with the superclass, otherwise you would not be able to use inheritance (because each class already extends Object).


  • I still don't understand how this is related to the above problem.


    I didn't think this through. Thanks Roel and sorry for so many questions
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic