1

why below code is not working ? whereas every class is extended from Object class.

try {
// simple code , throw some exception
} catch (Object e) { 
// handle the exception
}

If Exception class is inherited from object class than why Object reference is not allowed in catch block ?

If anyone knows answer of the above question than please let me know.

Anuj Verma
  • 23
  • 6

4 Answers4

11

The argument to a catch clause must inherit from Throwable

This constraint was put into the Java language from the start and is part of the promise to JRE writers that they can leverage in case it helps them make things go faster.

Mel Nicholson
  • 3,225
  • 14
  • 24
7

From the javadocs:

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. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

Read more here: http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

If Object where accept on the catch clause, something like this would actually work:

try {
    if (needToThrow) { 
        List<String> messages = new ArrayList<String>();
        list.add("a nice message");
        list.add("another message");
        list.add("random message"); 
        throw messages;
    } else {
        //do other stuff here
    } 
} catch (List list) {
    System.out.println("Got a list of messages, lets handle them");
}

Yet that would be a very strange way to control the execution flow, something that a simple if statement would solve.

So, the try and catch mechanism is not for doing basic flow control, but handling unexpected results. When a part of the code hits an unexpected result, it throws a message indicating that he cannot continue, he will abort the next statement and return the execution to the scope above. Hes basically saying I received an error and cannot continue, someone handle this situation. For someone to deal with this situation, resources needs to be gathered: the JVM will make a snapshot of the whole stack and put it inside a Throwable object. Thats why you can only catch something that is a Throwable. Also the above example would perform poorly as error resources would be gathered where it is not necessary.

On the two known Throwable types

Situations where the program can recover from are classified as Exceptions, like trying to open a missing file, connecting to somewhere that is not reachable, trying to access a wrong URL and so on.

When the program cannot recover, those situations are classified as Errors, like running out of memory or running into a compiling error.

Although you could still write your own class that inherits from Throwable, the two above types already cover all the possible error types (being recoverable or being not recoverable), so you probably wont see anything being catch that are not from any of those.

Also, catching a Throwable is a bad practice (Can you handle something that cannot be handled like Errors?). When you want to "catch anything in my code", catch Exception

Bruno Penteado
  • 2,234
  • 2
  • 23
  • 26
0

You seems new to programming. Anyways, Object is the root of java class hierarchy and from that hierarchy Throwable hierarchy is defined whose descendants are used to throw or get thrown itself if there is a related problem.

Basically what you need to learn is only errors and exceptions can be used in catch block because we track the problem occurred not the object that cause the problem.

So if u get ArrayOutOfBoundsException(which is derived from Exception& Exception is derived from Throwable) that means an array object is the reason for this Exception and better exception handling involves catching of specific problem.

I assume i helped you , if it really helped then hit my answer as acceptable.

Sachin Verma
  • 3,712
  • 10
  • 41
  • 74