• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Barry Boone - Question #28 - Ambiguous

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is yet another example of an ambiguous question that could mislead:

My Answer: a, b, c
Correct Answer and explanation:

I assumed that Exception1 and Exception2 extended Exception. The given answer would be correct only if Exception1 and Exception2 extended RuntimeException.

[This message has been edited by Betty Reynolds (edited April 21, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Betty:
IMO, the answer 'c' is correct. Since Thirds' test()
overrides Seconds' test() and Seconds' test() does
not throw any exceptions, Thirds' test() cannot
throw any exceotions either. It does not matter what
Seconds' test() overrides.
I agree with your explanation abt the ren time exception
stuff. But I am not sure how professional java programmers
use the throws construct. Do they use the throws construct
to declare Runtime exceptions ? I hope not ....
I hope some professional working in java could answer
these questions.... I am not for one...
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I am not sure how professional java programmers
use the throws construct. Do they use the throws construct
to declare Runtime exceptions ? I hope not ....

I never worry about catching or throwing MOST runtime exceptions - there are just too many to worry about - you would be severely clouding up your code if you tried to catch them all. Besides, there are some that you just can't do too much about like out of memory exceptions. I think that most books tell you not to worry about them with few exceptions (pun intended!)
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java Nut:
having said that...if i can ask you one more qstn....
given the above scenario, can I ASSUME that Exception1
and Exception2 in method test() of class First are
CHECKED EXCEPTIONS?
Regds.
- satya

 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
satya5,
I don't think you're getting my point. If you answer a, b and c, then don't go running for Chapter 8, because your instincts are probably correct. This is the better answer.
Just look at his own answer:
A method in a subclass cannot add new exception types that it might throw. Since it�s superclass, Second, does not define any exceptions in its test() method, Third can�t either. (See chapter 8.) This implies that a & b are correct also.
The only way that a & b might be incorrect is if Exception1 and Exception2 extend RuntimeException, in which case you won't get an error and then answer c only is valid. It's not likely that this would happen, as it's not good practice to extend RuntimeException. If you are going through the trouble to set up an exception, you probably want to extend Exception.
The point is what would happen in this case would depend on how you design the Exception. If you are asking if just extending Exception would make these fall into the category of checked exceptions, you won't find the answer in the book. You just have to try out an example yourself. The answer is yes.



 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Betty:
This is what I understand :
(Overriding classes cannot throw any new checked exceptions.)
If Exception1 and Exception2 are Runtime exceptions, the test()
method in class Third can throw them without errors. In that
case, my answer would be a,b,c.
If Exception1 and Exception2 are Checked exceptions, the test()
method in class Third CANNOT throw them. My answer is c.
Based on this understanding and Java Nuts's posting my
previous post says
"Can I ASSUME Exception1 and Exception2 to be Checked
exceptions?"
I hope I am clear now. Sorry for adding to ur confusion.
Are we one the same page ......
Regds.
- satya
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
satya5,
I see your point now. I completely misread the question.
a & b are right only if they are defined as runtime exceptions.
Sorry for the confusion, but it was a confusing question.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Betty:
now that we both agree....lets see if we are safe
making the assumption that "Exception1 and Exception2 are
Checked exceptions."
Any professional out there ....
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a and b are false, then exception 1 & 2 MUST be checked exceptions. If a and b are unchecked, then answers a and b MUST be true, as unchecked exceptions do not affect the inheritance heirarchy for overriding methods
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what we are looking for JavaNut, is confirmation that any user defined exception that extends Exception will, by default, be a checked exception. We've only confirmed that programmatically. Thanks.

 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this time I thought I would save the sheriffs some work. I've confirmed this myself from the JLS:
Section 11.2 The Compile Time Check of Exceptions

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.

(I wish I could link you to the section, but I only know how to link to the document page, so I copied the relevant statement here. Maybe Jim could share this secret with us sometime.)
My practical interpretation of this is that unless you are extending RuntimeException or Error or any of their subclasses, then by default, your exception class is checked.
This means that in extending Throwable, as well as Exception or any of its checked subclasses, your class is defined as checked.
I hope that's good enough satya.

[This message has been edited by Betty Reynolds (edited April 21, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Betty, that's a closely guarded secret. If I told you, we would have to delete all your posts. But if you follow the link from the UBB Code is ON note which comes up while you're composing a message (to the left of the composition area), you will find that the secret isn't always so carefully guarded.
(You'll also need to figure out how to copy and paste the URL of whatever you want to link to using your browser - if you're not familiar with this, ask away and someone can doubtless help.)
Another useful trick for finding out how to do things on this forum - when you see a post with an effect you like, you can look at what they typed to achieve it, by selecting the edit icon on their post. You'll see a screen which contains the "original" version of their post, which looks like it will allow you to edit it. Actually if you try to submit any changes, you will be told you don't have permission to do so - but the edit screen is still useful for a behind-the-scenes look at what's going on. Soon you too will be changing colors and fonts like Maha .
Oh yeah - your answer was exactly right of course - thanks.
 
pie. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic