Exception Handling in Java

Posted in /  

Exception Handling in Java

Ramya Shankar
Last updated on June 20, 2024

    This is the most significant procedure in Java that allows you to handle the runtime errors to maintain the normal flow of the application or the program. An exception is an unexceptional condition that occurs to disrupt the application or the program flow.  With the help of exception handling, you can manage the runtime errors that occur during the execution of the program.


    You will be able to maintain the normal program flow with exception handling. For example, if there any error occurs in between the program, then the rest of the program will not be executed but if we handle that exception, then you can run the remaining statements f the program.

    Java exception class hierarchy

    The java.lang.throwable is the base class that is inherited by two subclasses- exception and error. Below is the hierarchy of the java exception classes:

    Java exception class hierarchy

    Different types of Java exception

    The three different types of exception are-

    Checked exception

    The class which will inherit the throwable class directly are known as the checked exception and is generally checked at the compile-time.

    Unchecked exception

    The class that will inherit the RuntimeException are known as the unchecked exception and is checked at the runtime.


    Error is irrecoverable and will occur if the flow of the program failed.

    Java exception keywords-

    • Try- the try is a keyword that will specify the code block where the exception code will be placed. You cannot use the try block alone; it should be followed by either catch block or the finally block.
    • Catch- this block will be used to handle the exception that is thrown within the try block. Also, you cannot use the catch block alone; it should be preceded with the try code block and should be followed by the finally block.
    • Finally- this block will execute the critical code irrespective of the exception raised or not.
    • Throw- using this keyword you can throw an exception.
    • Throws- you can declare exceptions using the throws keyword.


    import java.util.regex.*;  
    public class Simple{  
    public static void main(String args[]){  
          int data=100/0;  
       }catch(ArithmeticException e){System.out.println(e);}   
       System.out.println("remaining code");  


    Exception Handling Output

    People are also reading:

    Leave a Comment on this Post