• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Exception in finalize()

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me what happens if an uncaught Exception
is throw in Overriden finalize().
I wanted to know whether the Object will be Garbage collected
or not
Thanks.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though the exception is raised the
object will be garbage collected.
if the finalize method in your
progam throws an exception it must
be enclosed in an try catch block.
hope it helps!.
 
Kishore Pamu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though the exception is raised the
object will be garbage collected.
if the finalize method in your
progam throws an exception it must
be enclosed in an try catch block
otherwise it gives a compiler error.
hope it helps!.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<<if the finalize method in your <br /> progam throws an exception it must <br /> be enclosed in an try catch block.<br /> >>>
if we override the finalize method (protected void finalize throws Throwable),we can make it to throw any exception in the throws clause ,so its not necessary to have a try-catch block as the above sentence says.
Please correct me if I am wrong.
Mousami
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
Let me explain two things about exceptions :
1) If a method throws a checked exception then the method must
have a try catch contruct , or, the method should declare that
it throws a checked exception so that the calling method can
appropraite action.
2) Runtime exception need to be caught. JVM takes care of it.
Now, having said that, lets see what happens when you override the <code>finalize()</code> method of the Object class. First of
all you must make sure to call the base class's (i.e, Object) finalise() , by making a call to <code>super.finalize()</code>.
If the finalize() method throws a checked exception then it should declare that it throws so that JVM can take appropraite
safegaurds. Else the overloaded finalize() method should itself
take steps to handle it by having a try catch block.
Hope this clears the issue.
Ravindra Mohan.
 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Question still remains :-
Even if u provide try Catch - Will the Object be GC
& what would be the case if u don't provide a call to Super.finalize();
Thank
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shah,
According to the API


If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.


Based on the above, any exceptions, wether thrown or caught, will be ignored.
If the gc determines that the object is unreachable it will be gc'd; so even if the finalize method does not complete ie terminates because of an exception, the object will still be gc'd if it has no references.
You do not have to call the superclass finalize method however it is considered good programming practice to do so.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic