Java Exception Types

Introduction

All exception types are subclasses of the built-in class Throwable.

Throwable has two subclasses which group exceptions into two distinct categories.

One group is headed by Exception and the other group is Error.

Exception is the class that you will subclass to create your own custom exception types.

RuntimeException is a subclass of Exception. It is normally thrown by JVM.

Error are used by the Java run-time system.

Stack overflow is an example of such an error.

Uncaught Exceptions

This following program includes an expression that intentionally causes a divide-by-zero error:

public class Main {
  public static void main(String args[]) {
    int d = 0;
    int a = 42 / d;
  }
}

Here is the exception generated when this example is executed:

Exception in thread "main" java.lang.ArithmeticException: / by zero
  at Main.main(Main.java:4)

The stack trace shows the calling hierarchy.




PreviousNext

Related