• 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

try-catch-finally block -- help

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a try block exist without a catch block if the code inside the try block actually throws an exception? What if the try block does not throw an exception?
Also, does normal exception handling occur even in the finalize() method? Some books say that exceptions are ignored in finalize() function, some books say otherwise.
Another thing is : is it recommended or required to call superclass' finalize() method in the subclass (if the subclass overrides the finalize() method)?
Please help, thank you very much.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no... if it is not unchecked exception... if a try block can throw a checked expception it should be able to catch it...
what i can say is that if u want to run finalize method of the super class then u have to explicitly call the fianlize method of super class... but compilier doesn't check.. so even if u haven't called finalize method of super class compiler will not complain....
hope it will help
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A try has to have either an associated catch OR finally; or it can have both.
The presence of an exception is a bit of a different subject. The compiler will always complain if a checked exception is generated and not caught or thrown, regardless of the try/catch/finally blocks.
You could have:
<code>
void exMethod() throws java.io.IOException
{
try
{
throw new java.io.IOException();
}
finally
{
System.out.println("finally");
}
}
</code>
and it would be up to the caller of exMethod to either catch or throw the exception.
Or you could have:
<code>
void exMethod()
{
try
{
System.out.println("try");
}
finally
{
System.out.println("finally");
}
}
</code>
But you can't have only a try.

[This message has been edited by Guy Reynolds (edited August 17, 2001).]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This are some points worth remembering !!
(1) try block without atleast one catch block or finally block after it gives compile error.
(2) Any exception thrown by the finalize method causes the finalization of that object to be stopped, but is not notified to the application and that object is still considered as finalized.

(3) Unlike constructors, which are chained from the subclass to superclass, finalizers are not implicitly chained. It is considered a good programming practice if you call the superclass finalizer like super.finalize() in your subclass.
-Sandeep
------------------
www.tipsmart.com
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the superclass's finalize to run then you have to call the superclass's finalize... finalize is not "chain called" like default constructors.
 
Christy Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, y'all...
 
reply
    Bookmark Topic Watch Topic
  • New Topic