• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Throwable is a class?  RSS feed

 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I mean, it name looks like name of interfaces in java.lang (Cloneable, Runnable, etc.) and its purpose looks more like interface purpose. But Throwable is a class rather then an interface. Why it's so?

First I thought that this is because of Throwable has some native code to deal with stack untwisting and stuff. But in that case, why Throwable is not an abstract class?

Thanks.
George
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is indeed a class because it includes "magic" native methods to deal with stack traces. Why isn't it an abstract class? Because it's perfectly legal -- if bad form -- to say

throw new Throwable("Something really bad happened.");
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Why isn't it an abstract class? Because it's perfectly legal -- if bad form -- to say

throw new Throwable("Something really bad happened.");


And what's the point? If I want checked exception I have to extend Exception. Unchecked exception - have to extend RuntimeException or Error.

I cannot imagine situation when I prefer to use Throwable (which have to be treat like checked exception with try/catch or throws) instead of Exception.

Can you give me a sample?
[ July 18, 2005: Message edited by: George Bolyuba ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Bolyuba:


And what's the point?


No point.

This is one of those examples, I think, of how the original designers of the core Java API didn't get everything absolutely right. In general, since they can't fix things like this without breaking existing code, they've never fixed them. Other favorite examples: StringBuffer should override equals() and hashCode(), but doesn't; Stack shouldn't extend Vector, but rather have a Vector member; the whole wretched design of clone(); Thread.sleep() being a static method; the list goes on and on.

As a rule of thumb, I wouldn't look to the essential core Java APIs themselves for examples of how to design Java classes. Remember that they were designed by people with very little experience programming in Java!
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

As a rule of thumb, I wouldn't look to the essential core Java APIs themselves for examples of how to design Java classes. Remember that they were designed by people with very little experience programming in Java!


Was added to memories.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!