aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Inheritance Hierarchy 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 "Exception Inheritance Hierarchy" Watch "Exception Inheritance Hierarchy" New topic
Author

Exception Inheritance Hierarchy

Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
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.


SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Apr 29, 2002
Posts: 200
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 ]

Be Afraid...Be very Afraid...
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
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)


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
It is defenitely

--> b. java.lang.Throwable.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
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

Joined: Nov 05, 2002
Posts: 13
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.


SCJP1.4<br />SCJD1.4 (preparing)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 29, 2005
Posts: 71
Originally posted by Parameswaran Thangavel:
hi mi mohammed

the folowing code giving error saying illegal start of type



- Jeff
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
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

Joined: Mar 29, 2005
Posts: 71
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

Joined: Apr 03, 2005
Posts: 30
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Inheritance Hierarchy
 
Similar Threads
Question #226 -- wrong answer
Possible Java Rule Round-up Ambiguity
Exception question
Error in question at The JavaRanch Rules Round-up Game
Exception handling