• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions & overriding?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi :
i have a problem with a question from the K&B book (Q 16 page 422) :




When fragments I - IV are added, independently, at line 10, which are true? (Choose all that apply.)
A. None will compile
B. They will all compile
C. Some, but not all, will compile
D. All of those that compile will throw an exception at runtime
E. None of those that compile will throw an exception at runtime
F. Only some of those that compile will throw an exception at runtime

Answer:
C and D are correct. An overriding method cannot throw checked exceptions that are
broader than those thrown by the overridden method. However an overriding method can
throw RuntimeExceptions not thrown by the overridden method



now , when i try to compile , it does compile with all the fragments added, independently !!
can you explain this please ? thank you
 
Daddaoua marouan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Idea ?!!!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all PatienceIsAVirtue. Your question has only been online for a couple of hours. Remember that there are a lot of members that live in different time-zones.

Option 2 does not compile because MyException is an Exception and not a RuntimeException. Thus it's throwing a larger Exception than the parent class which is illegal.
 
Daddaoua marouan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
holaaa i think i get it ... i don't know hours ago i tought it's compiled !!!

whene the overrided method doesn't throw ANY Exception AT ALL we assume that it throws RunTimeException ... is my logic correct ?! thus the overridING method should throw only RunTimeException or one of its kids !! please say yes !
and thank you
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daddaoua marouan wrote:
C. Some, but not all, will compile
D. All of those that compile will throw an exception at runtime

Answer:
C and D are correct.



Options D should be incorrect.
The option void doStuff(); compiles without error and doesn't explicitly mention about throwing exception at runtime.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daddaoua marouan wrote:
whene the overrided method doesn't throw ANY Exception AT ALL we assume that it throws RunTimeException ... is my logic correct ?! thus the overridING method should throw only RunTimeException or one of its kids !! please say yes !
and thank you



In reality, Java code can throw RuntimeException at any point, as it happens in RunTime.
But it's not explicitly mentioned in the method definition.
While overriding, one can throw only narrower checked exception or new RuntimeException.
 
Daddaoua marouan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean by Narrower : a subclass ?
so my logic is correct ?!
 
Daddaoua marouan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no no ... a Devision slash "/" was removed .. while copying from the pdf i guess
it should be like this




so D is correct , it will produce "/ by zero " ArithmeticException exception


sharp mind though
thank you
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daddaoua marouan wrote:
the overridING method should throw only RunTimeException or one of its kids



This logic is incorrect. As overriding method can ALSO throw no or same or narrower exception.
For example, consider the following code:


The DerivedClass can throw same exception:


Or narrower exception:


Or RuntimeException:


Or no exception at all:

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

Daddaoua marouan wrote:
whene the overrided method doesn't throw ANY Exception AT ALL we assume that it throws RunTimeException



I'm not sure whether we can assume it.
Any line of code can throw a RuntimeException in Java at Runtime, but it need not be explicitly mentioned.
I think we should not say any block of code throws RuntimeException, unless it explicitly says it throws it.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daddaoua marouan wrote:no no ... a Devision slash "/" was removed .. while copying from the pdf i guess
it should be like this




so D is correct , it will produce "/ by zero " ArithmeticException exception


sharp mind though
thank you



In that case, option D is correct.
We - being humans - know that it would throw ArithmeticException at runtime.
But compiler wouldn't know that at Compile time.
 
Daddaoua marouan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i totally GET IT ... thank you so much Trivikram AND Wouter Oet for your time and your well explanation
one more question if possible : did you finish the book or not yet ? if yes how long did it take
i ask because i think i'm hurring a little ... 10 days and i'm already at chapter 6 !!!
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daddaoua marouan wrote:Now i totally GET IT ... thank you so much Trivikram AND Wouter Oet for your time and your well explanation


Anytime

Daddaoua marouan wrote:did you finish the book or not yet ? if yes how long did it take


I finished the book about a month back. It took about 2 weeks to go through all chapters.
Right now, I'm reading Khalid Mughal. It's good for understanding Generics and Collections.

Daddaoua marouan wrote:10 days and i'm already at chapter 6 !!!


You need to hurry, as you need to solve some mock tests also.
 
Daddaoua marouan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you think so ? .. let me put some of my backgrounds and then tell me

i finished my 3 years this summer ... i had a 3 months course in JAVA .. (2 years with C , VB , matlab ....NO C++)
this summer i did an application for a moulding company (that's was a training) with java which is a Manifacturing Excution System .. i used MVC 2 pattern and programmed with netbeans IDE
but to be honest the IDE was of a great help .. somtimes i deployed things without undestanding it

voila ... that is ALL my experience with Java
am i still rushing ?


ps : sorry admins , i know this is a little out of the subject
 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:

Option 2 does not compile because MyException is an Exception and not a RuntimeException. Thus it's throwing a larger Exception than the parent class which is illegal.



Why does option 4 compile and option 2 does not?

Is it because an ArithmeticException is a subclass of RuntimeException? if so - how can you tell?

Thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I understand Trivikram Kamat explanation, it makes perfect sense.
However, in the initial example, method dostuff() in Tire does not throw any exception, thus the overriding method dostuff() in subclass ReThread should throw anything (since anything is broader than nothing ;) ) ...
I can't understand how this compiles, if someone could please enlighten me...
 
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Charis,
doStuff() in Tire does not throw any checked exception, so the overriding doStuff() in its child class should not throw any checked exception. But this overriding doStuff() method can still throw unchecked exceptions, such as runtime exception or its child class Arithmetic exception.
 
Charis Sfyrakis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, now it makes sense to me, i didn't realize that the Arithmetic exception is a subclass of the runtime exception...
Thanks for the clarification!
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer to our previous post:
Overriding posted on Feb, 17,2012
for more information about exception and overriding methods.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic