aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try-catch-finally block -- help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try-catch-finally block -- help" Watch "try-catch-finally block -- help" New topic
Author

try-catch-finally block -- help

Christy Smith
Greenhorn

Joined: Aug 15, 2001
Posts: 26
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

Joined: Aug 09, 2001
Posts: 39
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

Joined: Oct 27, 2000
Posts: 61
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

Joined: Dec 06, 2000
Posts: 57
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


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
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

Joined: Aug 15, 2001
Posts: 26
Thank you, y'all...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try-catch-finally block -- help