Exception Handling in Java

What is Exception Handling in Java?

Exception Handling in Java is a mechanism to handle runtime errors so that the normal flow of the program will be maintained.  Java has multiple types of Exceptions.

What is an Exception?

An exception in java is unwanted or unexpected behavior that occurs at the runtime and disturbs the flow of the program. 

Exceptions can occur due to many reasons such as invalid user inputs, invalid arithmetic operations performed, accessing invalid files, etc. to handle these cases we can use Exception Handling in java.

Throwable class in Java:

The Throwable class is the superclass of all errors and exceptions in the Java. Only objects that are instances of this class (or one of its subclasses) can be thrown by the Java throw statement and can be caught in the catch clause.

Constructors for Throwable class:

  1. public Throwable(): Creates a new throwable with null as its detail message.
  2. public Throwable(String message): Creates a new throwable with the specified detail message.
  3. public Throwable(String message, Throwable cause): Creates a new throwable with the specified detail message and cause.
  4. protected Throwable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace): Creates a new throwable with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled.
  5. public Throwable(Throwable cause): Creates a new throwable with the specified cause and a detailed message of (cause==null ? null: cause.toString()).

Exception Class in Java:

An exception is a subclass of Throwable which indicates conditions that a reasonable application might want to catch.

An exception has two types checked and unchecked exceptions. Classes that are not the subclass of RuntimeException class are known as checked exceptions.

Constructors for Exception Class:

Constructors of the Exception class are the same as the Throwable class as shown below.

  1. public Exception(): Creates a new exception with null as its detail message.
  2. public Exception(String message): Creates a new exception with the specified detail message.
  3. public Exception(String message, Throwable cause): Creates a new exception with the specified detail message and cause.
  4. protected Exception(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace): Creates a new exception with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled.
  5. public Exception(Throwable cause): Creates a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()).

Error Class in Java:

Error class is another subclass of Throwable in java. This class indicates serious problems that a reasonable application should not try to catch. These Errors are not required to be caught since these errors are abnormal conditions that should never occur. Error and its subclasses are considered unchecked exceptions.

Constructors for Error class:

  1. public Error(): Creates a new error with null as its detail message.
  2. public Error(String message): Creates a new error with the specified detail message.
  3. public Error(String message, Throwable cause): Creates a new error with the specified detail message and cause.
  4. protected Error(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace): Creates a new error with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled.
  5. public Error(Throwable cause): Creates a new error with the specified cause and a detail message of (cause==null ? null : cause.toString()).

-A blog by Shwetali Khambe

Related Posts