File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes finalize() Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finalize() Exception" Watch "finalize() Exception" New topic

finalize() Exception

Parag Mokal

Joined: Jan 14, 2001
Posts: 19
In the follwing class the "protected void finalize()" method gets compiled
public class FinalException {
FinalException() {
System.out.println("Creating FinalException ");
protected void finalize() {
System.out.println("finalizing ");
public static void main(String[] args)
new FinalException();
But when I call the "superclass finalize()"(which is commented in the above code method I get the error that the Throwable exception must be caught. Why the Exception has to be caught only when the superclass finalize() is called.
Elisabeth Van
Ranch Hand

Joined: Feb 09, 2001
Posts: 42
If you check the API, the finalize method in the Object class (which is what you're calling when you say super.finalize() ) throws a Throwable exception.
You don't get an error when you call the finalize method in your class, because it doesn't throw any exceptions. If you declared your method as follows:
protected void finalize() throws Throwable {
then you would have an exception you'd need to catch, as in the finalize() method from Object.
Hope this helps.
It is sorta covered in the JavaRanch Style Guide.
subject: finalize() Exception