Exception Types
Tags: java, programming, learning, beginners
Published: 2024-06-14 08:49:16

Exceptions are objects, and objects are defined using classes. The root class for exceptions is **java.lang.Throwable**. The preceding section used the classes **ArithmeticException** and **InputMismatchException**. Are there any other types of exceptions you can use? Can you define your own exception classes? Yes. There are many predefined exception classes in the Java API. Figure below shows some of them.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1bchg20atpktzq6he47.png)

The class names **Error**, **Exception**, and **RuntimeException** are somewhat confusing. All three of these classes are exceptions, and all of the errors occur at runtime.

The **Throwable** class is the root of exception classes. All Java exception classes inherit directly or indirectly from **Throwable**. You can create your own exception classes by extending **Exception** or a subclass of **Exception**.

The exception classes can be classified into three major types: system errors, exceptions, and runtime exceptions.

- _System errors_ are thrown by the JVM and are represented in the **Error** class. The **Error** class describes internal system errors, though such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. Examples of subclasses of **Error** are listed in Table below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxrrjvxb116ax1wcqmoi.png)

- _Exceptions_ are represented in the **Exception** class, which describes errors caused by your program and by external circumstances. These errors can be caught and handled by your program. Examples of subclasses of **Exception** are listed in Table below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiryu6gjnv8mvadjhste.png)

- _Runtime exceptions_ are represented in the **RuntimeException** class, which describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the JVM. Examples of subclasses are listed in Table below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nmkat35uqqqhblpkol1.png)

**RuntimeException**, **Error**, and their subclasses are known as _unchecked exceptions_. All other exceptions are known as _checked exceptions_, meaning that the compiler forces the programmer to check and deal with them in a **try-catch** block or declare it in the method header.

In most cases, unchecked exceptions reflect programming logic errors that are unrecoverable. For example, a **NullPointerException** is thrown if you access an object through a reference variable before an object is assigned to it; an **IndexOutOfBoundsException** is thrown if you access an element in an array outside the bounds of the array. These are logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in a program. To avoid cumbersome overuse of **try-catch** blocks, Java does not mandate that you write code to catch or declare unchecked exceptions.