Exception Handling in Java: Hierarchy Example and Types

One of the important topic Exception handling in java

Here we will discuss basics of exceptions, Why Exception handling and its types.

What is Exception in Java

A unwanted event that disrupts the program execution.

In Java all Exceptions are class.

Why Exception Occurs

  1. Due to wrong user input
  2. Incorrect programming logic

Example of Exceptions

  1. Dividing a number by zero 5/0 . It will throw ArithmeticException
  2. Converting a String to number Integer.parseInt("123s"). Exception It will throw NumberFormatException 
  3. Accessing beyond index in Array a[100] when only 5 elements are in array. Throw ArrayIndexOutOfBoundsException

ArithmeticException on Java Program

What is Exception Handling in Java

Exception can terminate the program execution.

To handle unwanted event and to execute rest of the program exception handling is required

On exception Java shows programmer friendly message. To show end used we need to provide custom message

How to Handle Exception in Java

Java provides keywords to handle the exception

Java exception can be handle by 5 keyword  try, catch, throw, throws and finally.

  1. try : Any Program statement that we want to check for exception must be placed  within a try block.
  2. catch  :  If the exception occurs within try block it is thrown. Your code can catch this exception (using catch) and handle it.
  3. throw  :  if an exception arises then this system-generated exception is automatically thrown by Java run time system. To manually throw an exception, throw keyword used.
  4. throws  :  A throws clause lists the type of exception that a method might throw.
  5. finally  :  In programming, we want any code must be executed after the try block completes is placed within a finally block.

Syntax:

try block can be used either with a catch or finally block or with both but try cannot be used alone.

A try can have multiple catch block.

When there is an exception in the try block an appropriate catch block is executed based on the exception.

Exception Hierarchy in java

In java programming, Exception class is a subclass of class Throwable.

Apart from the Exception class, class Error is also derived from class Throwable.

Java Exception Hierarchy
Fig: Exception Hierarchy

Error: In java programming, error is Unrecoverable. It occurs during compile time.

For example, if we write wrong syntax then error will generate during compile time.

Root class of exception in java

Throwable is the root class of exception in Java

Types of Exception in Java

  1. Checked Exception
  2. Unchecked Exception

Checked Exception in java

Exceptions that are checked by compiler is called checked exceptions.

On compiling above program.

Checked Exception in Java
Fig: Checked Exception

To make code executable we must surround Scanner with try and catch the FileNotFoundException.

Here is the list of some checked exception

  1. IllegalAccessException: in java programming, this exception occurs when access to a class is denied.
  2. ClassNotFoundException: this exception occurs when class not found.
  3. NoSuchMethodException : when a requested method dose not exist this exception occurs.
  4. CloneNotSupportedException: in java programming,If you are trying to use the clone method in a classwhere Cloneable interface is not implemented, it throwsCloneNotSupportedException. Clone() method is used to create exactcopy of a object
  5. InterruptedException : in java, when we work with multiple threads then one thread  interrupts another thread.  

Unchecked Exception in Java

Exception condition is not checked by compiler.

Runtime class and its sub classes are known as unchecked exception.

The compiler does not flag any error if there is an unchecked exception.

Unchecked Exception
Fig: Unchecked Exception compilation
  1. ArithmeticException : in java programming, this exception, represent the arithmetic error, such as divided-by-zero.
  2. IndexOutOfBoundsException: when some type of index is out of bounds this exception occurs.
    • ArrayIndexOutOfBoundsException: in java programming, this exception represents the Array index is out of bound. This condition arises when we try to access the illegal array index.
    • StringIndexOutOfBoundsException: in java programming, when we access the index outside the bounds of string then this exception occurs.
  3. NegativeArraySizeException: when an array is created with a negative size then this exception arises.
  4. NullPointerException: in java programming, this exception occurs when invalid use of a null reference.
  5. IllegalThreadStateException: this exception occurs when the required operation is not compatible with the current thread state.

Create your own Exception: User-Defined Exception in Java

What is difference between checked and unchecked exception in java

Checked Exceptions are checked by compile at compile time.

Unchecked Exceptions are not checked by the compiler.

Advantages of Exception Handling in Java

  1. Error handling is separate from Regular code
  2. Propagating Errors Up the Call Stack
  3. Grouping and Differentiating Error Types

Array index out of bound exception is checked or unchecked


array index out of bound exception is unchecked Exce

Read More

  1. Exception Handling in Java with Examples
  2. Exception Handling try catch finally blocks in Java
  3. Throw and Throws Keywords in Java

Reference

Advantages of Exceptions