File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declaring a method that throwsThrowable 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 "Declaring a method that throwsThrowable" Watch "Declaring a method that throwsThrowable" New topic

Declaring a method that throwsThrowable

J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Hello all,
can someone think of a reason as to why a method would be declared to throw a Throwable? i.e. why not just be restricted to Exception? After all, run-time exceptions don't have to be declared or caught?
Motivation: The finalize method of class Object.
thanks, Jamil
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
As far as I know, I think that the finalize method is the only one declaring a Throwable in its throws clause. The reason is because the finalize method is usually invoked by the garbage collection thread to give a chance to an object to clean things up. Now, if for some reasons, the clean up should not be completed successfully (error or whatever), it shouldn't affect the rest of the program and that's why Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.
I think developers should never declare Throwables in their throws clause but restrict it to the exact common denominator exception that gets thrown from the method.
[ April 15, 2002: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
They don't have to be.. but you can
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
thanks Val
I agree. Here's the link:
subject: Declaring a method that throwsThrowable
It's not a secret anymore!