Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try-catch-finally block -- help

 
Christy Smith
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
sriram gupta
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Guy Reynolds
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Sandeep Nachane
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, y'all...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic