All exception classes are subtypes of class Exception. This class derives from the class Throwable (which derives from the class Object).
In general, an exception represents something that happens not as a result of a programming error, but rather because some resource is not available or some other condition required for correct execution is not present. For example, if your application is supposed to communicate with another application or computer that is not answering, this is an exception that is not caused by a bug.
java.lang.Object
↳ java.lang.Throwable
↳ java.lang.Exception
↳ java.lang.RuntimeException
↳ java.lang.Error
Throwable
|-- Error
| |-- VirtualMachineError
| | |-- OutOfMemoryError
| | `-- StackOverflowError
| |-- LinkageError
| | |-- ClassCircularityError
| | |-- ClassFormatError
| | `-- NoClassDefFoundError
| `-- AssertionError
|
`-- Exception
|-- IOException
| |-- FileNotFoundException
| `-- EOFException
|-- ReflectiveOperationException
| |-- ClassNotFoundException
| |-- IllegalAccessException
| `-- InstantiationException
|-- RuntimeException
|-- ArithmeticException
|-- ArrayStoreException
|-- ClassCastException
|-- IndexOutOfBoundsException
| |-- ArrayIndexOutOfBoundsException
| `-- StringIndexOutOfBoundsException
|-- NullPointerException
|-- IllegalArgumentException
| `-- IllegalThreadStateException
|-- NumberFormatException
`-- UnsupportedOperationException
As you can see, there are two subclasses that derive from Throwable: Exception and ErrorChecked Exceptions: These are exceptions that are checked at compile-time. The compiler ensures that these exceptions are either handled using a try-catch block or declared in the method's throws clause.
Example: IOException, SQLException
Unchecked Exceptions: These are exceptions that are not checked at compile-time but occur at runtime. They are subclasses of RuntimeException.
Example: NullPointerException, ArrayIndexOutOfBoundsException
Errors: These are serious issues that a program typically cannot recover from.
Example: OutOfMemoryError, StackOverflowError
public class Example1 { public static void main(String[] args) { int num1 = 10; int num2 = 0; // Unchecked Exception // This will cause an ArithmeticException (divide by zero) int result = num1 / num2; System.out.println("Result: " + result); }}Exception in thread "main" java.lang.ArithmeticException: / by zero
at Example1.main(Example1.java:8)