Java Program to Implement Exception Handling.

Below code is used to implement exceptiuon handling:

import java.io.*;

class MyException extends Exception
{
    private int detail;
    MyException(int a)
    {
        detail=a;
    }
    public String toString()
    {
        return" Minor with age less than 18 and you are only "+detail;
    }
}

class ExceptionDemo
    static void compute(int x
    throws MyException
    {
        if(x<18)
            throw new MyException(x);
        else 
            System.out.println("you can vote\n");
    }
    public static void main(String arg[])
    {
        try
        {
            compute(20);
            compute(17);
        }
        catch(MyException e)
        {
            System.out.println("My Exception caught"+e);
        }
        finally
        {
            System.out.println("You are an Indian");
        }   
    }
}
Thank You.

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT