Thursday, March 16, 2017

Exception Handling Using Try Catch Java

Tags

The statements which can throw exception are placed in try block and after the exception is thrown the catch block following the try block are executed and the program is not abnormally terminated.

Code::

import java.util.*;
public class Exception 
{

    
    public static void main(String[] nt) 
    {
        Scanner in =new Scanner(System.in);
        System.out.print("Enter any Number:");
        try
        {
        int a=in.nextInt();
        int b=12345/a;
        System.out.println("Result is :"+b);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Exception: " +e);
        }
        
    }

}

Output::