Exception Handling in java

What is an Exception?

An Exception is an unwanted event that interrupts the default flow of the program. When some exception occurs program execution immediately terminated. In those cases, we get a system-generated error message.

The good part about exceptions is that they can be handled in Java. By handling the exceptions we can provide a meaningful message to the user about the issue rather than a system-generated message, which may not be understandable to a normal user.

Exception Handling Hierarchy of Java Exception classes

Mainly, Throwable class in java.lang is the root class of Exception Handling hierarchy which is defined by two subclasses: Exception and Error. A flow chart of the hierarchy of Java Exception classes are given below:

Types of Java Exceptions :

Mainly, there are only two types of Exceptions :

  • Checked Exception
  • Unchecked Exception

But from Oracle, there is another exception which is called Error.

Difference between Checked and Unchecked Exceptions

1) Checked Exception

The classes in java program which is directly inherited Throwable class except RuntimeException and Error are known as checked exceptions. For example IOException, SQLException, etc. And checked exceptions are only checked at compile-time.

2) Unchecked Exception

The classes which are inherited RuntimeException are known as unchecked exceptions. For example : ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. And unchecked exceptions are not checked at compile-time, but they are only checked at runtime.

Java Exception Keywords

Mainly, there are 5 keywords which are used in handling exceptions in Java :

1). try: This keyword is used to specify a block where we should place the exception code. And the try block must be followed by either catch or finally. It only means, we can’t use try block alone.

2). catch: This block is mainly used to handle the exception. It must be preceded by try block that means we can’t use catch block alone. It only followed by finally block later.

And another three keywords are: finally, throw and throws which we discuss below in detail so you can easily understand.

Difference between throw and throws in Java?

Throws keyword is mainly used to declare an exception in java, which means it works similarly to the try-catch block. And the throw keyword is only used to throw an exception explicitly.

If we go with syntax wise than throw is followed by an instance of Exception class and throws mainly followed by exception class names. For example :

throw new ArithmeticException("Arithmetic Exception");
// and
throws ArithmeticException;

And another difference is that throw keyword is used in the method body to throw an exception, while throws are used in the method signature to declare the exceptions that maybe occur in the statements present in the method.

For example:

//For Example Throw : 
void mythrowMethod() {
   try {
      //throwing arithmetic exception using throw
      throw new ArithmeticException("Something wanna wrong!!");
   } 
   catch (Exception exp) {
      System.out.println("Error: "+exp.getMessage());
   }
}


// For Example Throws:
//Declaring arithmetic exception using throws
void sample() throws ArithmeticException{
   //Statements
}
...

What is final, finally and finalize?

final: It used to apply restrictions on the class, method, and variable. A final class can’t be inherited, the final method can’t be overridden and the final variable value can’t be changed. And also the final is a keyword.

finally: It is used to place important code, it will be executed whether the exception is handled or not. And also finally is a block.

finalize: It is mainly used to perform clean up processing just before the object is garbage collected. And also finalize is a method.