Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring a method that throwsThrowable

 
J Hreich
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joshua Kueck
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They don't have to be.. but you can
 
J Hreich
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Val
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic