Frage

I am new to JAVA i am preparing for my OCP JP certification.

I have this doubt regarding exception handling

try{
        assert(false):"HI";
    }
catch(Throwable e){}

I know assert should not be handled but i am using this just for example. From what i know Object->Throwable ->Exception and Error

If i have Throwable or Exception or Error in the catch block it works but when i have object which is super class of all, eclipse shows me a compile time error.

Any reason why i cant have Object in catch block?

War es hilfreich?

Lösung

Only objects of type Throwable can be thrown or caught (which includes Exception and Error) in Java.

throw new Object(); // compiler error
throw new Integer(1); // compiler error

So it would be worthless to catch any old Object, because only Throwables can be thrown.

Quoting from the JLS, Section 14.20:

Each class type used in the denotation of the type of an exception parameter must be the class Throwable or a subclass of Throwable, or a compile-time error occurs.

Andere Tipps

According to java docs

The Throwable class is the superclass of all errors and exceptions in the Java language.
Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.
Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

Object is super class of Throwable,not the subclass.
Observe this class hierarchy.

enter image description here

You can only use Throwable or all its subclasses

As per JLS 14.20. The try statement

Each class type used in the denotation of the type of an exception parameter must be the class Throwable or a subclass of Throwable, or a compile-time error occurs.

Becaz in catch it expects throwable. Exception class or error class are throwable type as they are the subclass of Throwable. But Object class is not subclass of Throwable interface.

And important you can't throw an object. You can throw either an error or exception.

Everyone is saying that only throwable and its child class come into the catch block parenthesis which is right . Now ,your doubt is :- why object cant be there why is it designed that way ? i am breaking your question into 2 parts :- 1) why is it designed in that way ? Answer :-because , we can only throw throwable class and its child class . So , it is designed to catch only these type of object only . 2) why we cannot use Object class reference at the place of throwable or Exception Or Error ?

Answer :- It is very simple brother . throwable class is child class of Object class .ok . Now , we know that a Parent class can hold the reference of its child class but a child class can not hold the reference of its Parent class . Object class is parent class and throwable is child class and Catch block only accept throwable type . so , you can write throwable or Exception Or Error in catch block because throwable class is able to hold the reference of own and its subclasses . but throwable class can not hold the reference of its own Parent class i.e Object class . child class can not hold the reference of Parent class . I think this is the reason due to which we can not write Object type reference in catch block .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top