• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Most confusing question ever

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy all,

This question refers to the end of book Exam in the Gupta book. The confusing answer that does not make any sense to me, the reason I am making a new post, is given on page 465-466.

The question is:
Given the following code, which of the following statements are true? (Select 3 options.)


The answers choices are:
a The code will print caught
b The code won't print caught
c The code would print caught if StackOverflowError were a runtime exception.
d The code would print caught if StackOverflowError were a checked exception.
e The code will print caught if question() throws exception NullPointerException.


Answer: a, c, d

Answer a obviously makes sense but c and d as correct answers do not make sense to me especially considering Exception classes and Error classes are not related.

Can someone please help me understand why c and d are actually correct? 0_o

Regards,

Ted
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted North wrote:considering Exception classes and Error classes are not related.


Not quite correct; they are both Throwable.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Ted North wrote:considering Exception classes and Error classes are not related.


Not quite correct; they are both Throwable.


I see what you mean java.lang.Error classes and java.lang.Exception classes both extend java.lang.Throwable which extends java.lang.Object but I am not seeing how c and d are correct. 0_o

Thank-you for the reply Bear.

It seems like if StackOverflowError were a runtime exception or checked exception then it would not really catch a StackOverflowError since a runtime exception or a checked exception can not see anything in java.lang.Error. As you point out though if Throwable were used for the catch handler then it would print "caught".

Respectfully,

Ted
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted North wrote:It seems like if StackOverflowError were a runtime exception or checked exception then it would not really catch a StackOverflowError since a runtime exception or a checked exception can not see anything in java.lang.Error.

You''ll have to explain what you mean by that. Sounds like some odd misconception.

As you point out though if Throwable were used for the catch handler then it would print "caught".

Here, you have actually answered your own question. Both Exception and Error are Throwable, so if what you say is true...
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Ted North wrote:It seems like if StackOverflowError were a runtime exception or checked exception then it would not really catch a StackOverflowError since a runtime exception or a checked exception can not see anything in java.lang.Error.

You''ll have to explain what you mean by that. Sounds like some odd misconception.


Well,

Since Throwable is the super class of both Error and Exception classes they both share the same super classes but an Error and Exception are not related as they do not sub-class each other. There is not IS-A relationship between Error and Exception. So an error like java.lang.StackOverflowError can not be caught with anything in the Exception hierarchy of classes except for its super class java.lang.Throwable.

Bear Bibeault wrote:
Ted North wrote:As you point out though if Throwable were used for the catch handler then it would print "caught".

Bear Bibeault wrote:Here, you have actually answered your own question. Both Exception and Error are Throwable, so if what you say is true...



Yes, I understand this part. Class java.lang.Throwable can catch everything from Error and Exception classes including Runtime Exceptions.

Respectfully,

Ted
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted North wrote:So an error like java.lang.StackOverflowError can not be caught with anything in the Exception hierarchy of classes except for its super class java.lang.Throwable.

Not true. Any Throwables can be caught as any superclass in its class hierarchy (which this question does not specify), and as itself. So whether StackOverflowError is a checked or a run-time exception is actually moot; all Throwables can be caught.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Ted North wrote:So an error like java.lang.StackOverflowError can not be caught with anything in the Exception hierarchy of classes except for its super class java.lang.Throwable.

Not true. Any Throwables can be caught as any superclass in its class hierarchy (which this question does not specify), and as itself. So whether StackOverflowError is a checked or a run-time exception is actually moot; all Throwables can be caught.


Ah, I think I see the reasoning behind this question now. "All Throwables can be caught" makes sense to me. Thank-you Bear for your help with understanding this confusing problem for me.

Catching anything that IS-A java.lang.Error with any subclass of java.lang.Exception or java.lang.Exception itself still seems impossible to me though.

Regards,

Ted
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if StackOverflowError was a java.lang.Exception then it wouldn't be a java.lang.Error, would it? So the thing you don't think should happen wouldn't actually be happening.
 
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
I agree with a and c, but not with d!

If StackOverflowError was a checked exception, the code snippet as-is would not compile because the catch block would be unreachable (because it's never thrown by the try-block). So that's a mistake in the book and can easily fixed by adding throws StackOverflowError to the signature of the question-method.

Try it for yourself with an actual checked exception, like IOException:


So a similar change should be made to the question-method for option (d) to be correct!
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:But if StackOverflowError was a java.lang.Exception then it wouldn't be a java.lang.Error, would it? So the thing you don't think should happen wouldn't actually be happening.


Matthew,

I can visualize this after reading it a few times. Thank-you for the reply. So if I imagine that java.lang.StackOverflowError were actually a java.lang.RuntimeException then the code as it is would compile and run. Of course it would not run for long and eventually blow up at runtime, because of the recursive method calls, and then print "caught" after the exception handler takes control of the flow of code after which, the program would end.


Regards,

Ted
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:I agree with a and c, but not with d!

If StackOverflowError was a checked exception, the code snippet as-is would not compile because the catch block would be unreachable (because it's never thrown by the try-block). So that's a mistake in the book and can easily fixed by adding throws StackOverflowError to the signature of the question-method.

Try it for yourself with an actual checked exception, like IOException:


So a similar change should be made to the question-method for option (d) to be correct!


Roel,

Excellent reply. I did not think about how things would be different if java.lang.StackOverflowError were actually java.lang.StackOverflowCheckedException or something. It seems like the main method would also need to catch or declare that it also throws the exception too for it to compile. Although, this is not the case since the method that throws the exception handles it itself.

I made this program to try and figure out what you are talking about in your code. If the try block explicitly throws the exception in the try block there are no compilation issues either. Before I put in the explicit call to throw the exception I received the following compilation error

If I remove this line of code that throws the IOException in the try block and add a throws clause in the question method I then have to surround the initial call from main in a try/catch block or declare that it also throws IOException.



Thank-you again for the reply Roel. This does give one a great deal to think about. It does look like you found an error in the book as checkedExceptions do behave a differently than errors and runtimeExceptions that do not have to be handled to compile for instance.

Respectfully,

Ted
 
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
Ted North wrote:I did not think about how things would be different if java.lang.StackOverflowError were actually java.lang.StackOverflowCheckedException or something. It seems like the main method would also need to catch or declare that it also throws the exception too for it to compile. Although, this is not the case since the method that throws the exception handles it itself.

Yes, the main-method should also declare the throws StackOverflowError in its signature. Forget to mention that one in previous post.

Honestly I'm not a huge fan of this "what would happen if x would be y" kind of questions. When I did a (technical) review of the book I advised against this kind of questions. Learning Java is already a tough journey itself, such questions make it even harder (with little true benefit, beginning java programmers might even get more confused). Such questions are even a big risk for the book's quality due to possible unforeseen side-effects (as shown with this question).

For completeness: the code snippet of question 17 should be updated to the following code sample. With this updated code sample the correct answers are a, c and d.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

Thank-you for the positive feedback. This question does seem to be extra confusing particularly for beginning java programmers as you point out.

Your corrected code looks good maybe this will make it to the second edition? I helped proof-read some for this book. I guess they wanted a prospective test takers impressions of the book. This is probably one of the coolest "jobs" I have had. It was not really a job though since it was fun and I only received a free book and huge self-esteem boost. That is awesome that your name is on the back cover.

Respectfully,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic