In this tutorial, we will learn about the Exception Handling in Java. How to use them with the help of examples.

Exception Handling in Java

Exception handling is a process to trap the logical errors and manage it within program without terminating the flow of execution.

Error in Java

Error is the mistake happen in computer that produce irrelevant activities during compile or run time. The action on things that not fulfill the result is know as error.

There are two types of Error

  • Compile time error
  • Run time Error & Logical error

Compile time error

Compile time error happened during compilation of program due to typing mistake like leaving semicolon, writing wrong spell, etc. This error can be trap by the compiler during compile time and appropriate message. If program not successfully complied then it is not enable to run.

Run time Error & Logical error

Run time Error is the error occurs during running of program due to logical mistakes, means the result show on the screen is not appropriate as you required.

These are four basic action perform during exception handling.

  • Hit – observe that there is error.
  • Throw – returns that error.
  • Catch – receive that error.
  • Handle or manage – provide action on error.

How can we manage Exception in program?

Java provides a special block called try catch block to manage exception. Using the try— catch block, try can be written once but catch block can be one or more than one

Try block

Try {

  • All the basic operation come inside try block.
  • It is use Hit and Throw block.

}

Catch block

Catch (exception obj) {

  • Operation related with logic error.
  • It is use Catch and Handle block.

}

Syntax
try{    
//code that may throw an exception    
}catch(Exception_class_Name ref){}    
The Syntax of Java try-catch
Syntax
try{    
//code that may throw an exception    
}finally{}
The Syntax of try-finally block

Final Block in Java

This is also an optional block of try block. This is use with try block for executing the statements whenever exception is generate by try block or not. It means it is sure that finally block will be execute at any how whenever exception is generate or not or corresponding exception is define or not with catch block.

The finally block can be written at last after catch block or it can be written just after try block when catch is not define.

Example

import java.io.DataInputStream;
import java.io.IOException;

public class Errorm {
    public static void main (String args[])
    {
        DataInputStream z= new DataInputStream(System.in);
        int n[]= new int [4];
        int i;
        try
        {
            System.out.println("Enter" +n.length+ "number");
            for (i=0;i<n.length;i++)
            n[i]= Integer.parseInt(z.readLine());
            for (i=0;i<n.length;i++)
            System.out.println(n[i]);
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Bad Index" +e.getMessage());
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}
Example of Exception Handling

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *