• 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 In Finalizers

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finalize method in Object delcares to throw Throwble . Overriding methods can limit the range of thorwables to unchecked exceptions. Further overridden methods in subclasses WILL NOT BE ABLE be able to throw CHECKED exceptions. (as explanation to the review question 8.4)

Any exception thrown but not caught in by a finalizer invoked by the garbage collector is ignored. (Object Finalization - p.364)

These two paragraphs are from the Khalid Mughal Book. If the GC ignores any exception - my doubt is - why will subclasses not be able to throw checked exceptions. I often see this question on many mock exams asking whether overridden finalizers can throw any exceptions.

I hope someone here can clarify..
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Iam also reading the same book.
I think the reason is as follows.

See whenever a Checked exception is thrown the compiler will say either you have to catch it or u have to throw it. If not compiler will not allow you to go further.OK

Now come to finalize(),even though the exception occurs in finalize() the runtime ie JVM will ignore.This happens at runtime.
But if u dont satisfy the compiler how will u run the program. Thats why you have to declare it to be thrown or catch the Exception to pass on the compiler's test [in case of checked exception].
This is my thinking.
Let us see more replies.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Even check this thread..

Doubt in Finalize() - Khalid mughal book


Hope it helps you out..
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sankar,

the same doubt i too got ..and posted the same topic there the responders said that we can throw the checked exceptions also in case of overriden finalise method and i feel its true since the declaration of the finalise method says ..protected void finalise() throws throwable;

and regarding the ignoring ..what i feel after coming across one question in khalid..is that if the finalise method is called by GC(generally) then the exception is ignored immaterial of whether it is caught or thrown and the object is garbage collected .
whereas in case, if it called explicitly then it acts like normal method where we have to do our best(caught or throw) to handle it...

correct me if iam wrong

sri
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic