• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

finalize() method

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The finalize() method in Object can throw any Throwable Object. Overriding
methods can limit the range of throwables to unchecked exceptions. Further
Overridden definitions of this method in subclasses will not be able to
throw checked exceptions.



Is the above statement correct.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



How do u say "yes". In the above example I used an "Exception" in the
throws clause which is a "checked Exception". The compiler didn't complain about any thing.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look again at the second and third sentences you quoted: "Overriding methods can limit the range of throwables to unchecked exceptions. Further Overridden definitions of this method in subclasses will not be able to throw checked exceptions." Now I assumed that these two sentences went together. That is when they talk about "further overridden definitions", they mean further after you've already overridden with a method that limits the range of throwables to unchecked exceptions. You haven't done that, so you're getting different results than they describe.

Try this:

This won't compile, because Foo overrides finalize() in a way that limits the range to only unchecked exceptions. And Bar extends Foo. So finalize() in Bar can't throw any new checked exceptions. That's what the quoted text is talking about.
[ June 20, 2005: Message edited by: Jim Yingst ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you are applying the rule of overridding which say that we can override the method only with the same level of exception or subclass of the superclass exception.
isn't it

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by amit taneja:
i think you are applying the rule of overridding which say that we can override the method only with the same level of exception or subclass of the superclass exception.
isn't it



You cannot throw a more specific or an unrelated checked exception. Easy way to think of this is whether your override would break compilation of methods that already call it.

Base class:

void foo() throws IOException

Possible derived class overrides: OK

void foo() // OK
void foo() throws Exception // OK
void foo() throws IOException // OK
void foo() throws ArithmeticException // OK (unchecked)

void foo() throws SocketException // BAD (more specific)
void foo() throws SQLException // BAD (unrelated)
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rick O'Shay:


You cannot throw a more specific or an unrelated checked exception. Easy way to think of this is whether your override would break compilation of methods that already call it.



Does this explain the concept in K & B book,

'The overriding method must not throw new or broader checked exceptions than those declared by the overridden method' and

'The overriding method can throw narrower or fewer exceptions. Just because an overridden method �takes risks� doesn�t mean that the overriding subclass� exception takes the same risks. Bottom line: An overriding method doesn�t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.'

Im still not very clear about these two points which seems kind of contradict, especially for that 'Bottom line'

Could someone explain more? Thanks.
[ June 23, 2005: Message edited by: reubin nibuer ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means
1) the overriding method may not throw an exception even if overridden method does
2) if the overriding method thows an exception it should be either same or subclass of the exception thrown by overriden method
3) overriding method can not thorw a new checked exception
4) but overriding method can always thorw new uncehcked exception (ex: runtime excpetion)
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!