Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Inheritance Hierarchy

 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should be the answer to following question?


All exceptions inherit from:

a. java.lang.Error
b. java.lang.Throwable
c. java.lang.Exception


My answer (c), but correct answer for this question is (b) with justification that the exception hierarchy begins at java.lang.Throwable.

Somehow I wish not to agree this because all exceptions including runtime/checked are extended from Exception class. The throable class is one more level up hierarchy to support JVM errors too.

Please advise what should I answer to this question in real exam!

Thanks for help.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.Exception class is also a part of Exception classes, right? now what do u think should be the answer
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java API:

The Throwable class is the superclass of all errors and exceptions in the Java language.[

[ April 02, 2005: Message edited by: Paulo Aquino ]
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me try.
JLS says Throwable.
try this code.

class ScaryPal {
try {
throw new Exception();
}
catch(Throwable t)
{
t.printStackTrace();
}
public static void main(String [] args) {
new ScaryPal();
}
} // this showed clear the doubt(Exception propagation)
 
Saurabh Khanna
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is defenitely

--> b. java.lang.Throwable.
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saurabh Khanna:
It is defenitely

--> b. java.lang.Throwable.


But why? Are there any exceptions that do not inherit from Exception?

If not, then wouldn't b and c both be true?

- Jeff
 
Yingtao Jiang
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any exceptions that do not inherit from Exception?

Of course, you can define a myException Calss which extends directly from Throwable class. And throw a myException anywhere you think appropriate in your code.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mi mohammed

the folowing code giving error saying illegal start of type

class ScaryPal {
try {
throw new Exception();
}
catch(Throwable t)
{
t.printStackTrace();
}
public static void main(String [] args) {
new ScaryPal();
}
}


whats the error

any one please
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Parameswaran Thangavel:
hi mi mohammed

the folowing code giving error saying illegal start of type



- Jeff
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yingtao Jiang:

Of course, you can define a myException Calss which extends directly from Throwable class. And throw a myException anywhere you think appropriate in your code.


True. But, now that I've looked into it, perhaps what the question is getting at is that Throwable has two subclass: Exception and Error. Both of those can be thrown, and both indicate that "something exceptional" has happened.

So they seem to be classifying both Exception and Error as "exceptions" in this case (note the lowercase "e"). That's a pretty lousy way of putting the question though, if you ask me.

- Jeff
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh wait a minute... I think I finally figured this one out. Maybe.

Many exceptions do inherit from the Exception class. But some exceptions (not ones you make yourself, but actual, built-in Java exceptions) do not. Which ones? Ones that are Exception objects themselves!

So the statement "all exceptions inherit from Exception" is false, since an Exception is an exception, and you can't inherit from yourself.

The fact that you can catch an exception in an object of type Throwable is a bit beside the point. Any object can be referred to by a variable of the type of the object's superclass, not just Exception objects.

- Jeff
 
Saurabh Khanna
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Jetton:


But why? Are there any exceptions that do not inherit from Exception?

If not, then wouldn't b and c both be true?

- Jeff


Hi Jeff

Yes, The Error class and its subclasses (for example AssertionError, ThreadDeathError) are extended straight from Throwable.

And as mentioned in one of the previous posts by someone, you can make your own class extend Throwable as well...

Hope this is clear?

regards
Saurabh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic