My Project - Video Waker Alarm
Get Video alarm on Google Play

Example of exception handling with try catch in java


Example of exception handling with try catch in java. Exceptions are the undefined conditions that occur and are to be handled. Exceptions are called as run time exception. You can read the following article to know more about exception : What are exception and why exceptions are to handle and also difference between throw and throws keyword in java.

Example of exception handling with try catch in java

Example of exception handling with try catch in java
Example of exception handling with try catch in java

Here in the below example of an Arithmetic exception of divide by zero exception is demonstrated. This example is one of the general exception that what you might come across to most of books and internet.

Example of exception handling with try catch in java

import java.util.Scanner; 

class ExceptionDemo
{
    public static void main(String arg[])
    {
        int ans, div, d;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the dividend");
        div = sc.nextInt();
        System.out.println("Enter the divisor");
        d = sc.nextInt();
        ans = div / d;
        System.out.println("When "+div+" is divided by "+"d"+" it evaluates to "+ans);
    }
}

Output of Example of exception handling with try catch in java:

Example of exception handling with try catch in java
Example of exception handling with try catch in java

You can see in the above example that their is a possibility, that code may make an exception or may not, and it depends on many factors. One of the factor is, the input enter by user is valid or not. i.e in the output as shown above, the programs runs correctly when divisor value is 4 and evaluates to 5, while when divided by zero gives an exception when value of divisor entered by user is zero i.e 0.

In java, exceptions are handle by programmer with the help of try catch block. Their are some part of statement where exceptions may occur and that part of code is to be inserted in try block. Here we can see the above code, in which the code “ans = div / d”; might make an exception, when value of ‘d = 0’ . With try block, their should be at least one catch block associated after try block in the code.  When interpreter reach try block, it executes the code written in try and when  the code makes an exception, it throws an exception to catch block and the remaining line of code from where the exception occur in try block, will not get executed. After that only catch block will get executed. If no exception occur, then catch block remain unexecuted. Their can be multiple catch block with one try block. Here is the Example of exception handling with try catch in java.

import java.util.Scanner; 

class ExceptionDemo1
{
    public static void main(String arg[])
    {
        int ans, div, d;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the dividend");
        div = sc.nextInt();
        System.out.println("Enter the divisor");
        d = sc.nextInt();
        try 
        {
            ans = div / d;                                                  //code that might make an exception             
           System.out.println("When "+div+" is divided by "+"d"+" it evaluates to "+ans);
        }
        catch(ArithmeticException e)
        {
            System.out.println("You have wrong divisor as 0, which can not be evaluated");
            System.out.println("System by default taken it as 1, and evaluated as");
            ans =div / 1;
            System.out.println("When "+div+" is divided by "+d+" it evaluates to "+ans);            
        }
    }
}

Output of Example of exception handling with try catch in java:

Example of exception handling with try catch in java
Example of exception handling with try catch in java

As from the above output we can see that, even when user inputs zero or ‘0’ as divisor, the program does not make any run time error and executes correctly, by taking the divisor as 1 as coded in the catch block of above program. Well Exception handling does not end up here. A programmer may also use Finally block after using try and catch block,  contains java code that will run whether or not an exception is thrown in a try block. A separate post will discuss more about finally block.

If you are having any problem regarding exception handling in java using try catch block, do comment below.


Leave a Reply

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