Frage

What is the built in base class to handle all exceptions in Java? Is It Exception or Throwable?

What is the difference between two builtin classes,can someone explain me.

War es hilfreich?

Lösung

Below image will help you to understand Exception Hierarchy

enter image description here

Image Ref: programcreek:

As you can see Throwable is super class of Error and Exception while Exception deals with checked and unchecked Exception.

Exception

The term exception is shorthand for the phrase "exceptional event."

Throwable:

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.

Andere Tipps

The javadocs are for this. Here you can see that Throwable is the superclass for all Exceptions and Errors. Then you have checked and unchecked Exceptions, where the latter is RuntimeException and all its subclasses.

Remember to use Google when you're wondering about things like this, because all this information is widely available and easily found with a search engine.

From the javadocs:

Class Exception

java.lang.Object
 |
 ->java.lang.Throwable
    |
    ->java.lang.Exception

Hope this clears the doubt.

The above answered are very much informative. I just want to add:

  • The Master Base Class: java.lang.Object

  • The Master Exception Class: java.lang.Throwable

  • The Master Exception Class extends Throwable: java.lang.Exception and java.lang.Error

  • The Unchecked class extends Exception extends Throwable: java.lang.RuntimeException

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