Saturday, 19 October 2013

Program to throw your own exception

//Program to throw your own exception
//Developed By Ahlam Ansari
import java.util.*;
class InvalidDataException extends Exception
{
    InvalidDataException (String msg) //throws InvalidDataException
    {
        System.out.println(msg);
        //throw new InvalidDataException("Alert");
    }
}

class ApplicationForm
{
    String name;
    float percentage;

    ApplicationForm(String name, float percentage)
    {
        this.name=name;
        this.percentage=percentage;
    }
}
   
class ApplyAdmission
{
    public static void main(String ahlam[])
    {
        Scanner sc= new Scanner(System.in);
        System.out.println("Welcome to M.H. Saboo Siddik College of Engineering");
        System.out.println("...............Application Form.....................");
        System.out.println("Enter your Name:");
        String name=sc.next();
        System.out.println("Enter your Percentage:");
        float percentage=sc.nextFloat();
        ApplicationForm af=new ApplicationForm (name, percentage);
        try
        {
            if (percentage <80.00)
            {
                throw new InvalidDataException ("Sorry "+af.name+" your Application cannot be accepted.");
            }
            else
            {
                System.out.println(af.name+" your Application has been accepted.");
            }
        }
        catch(InvalidDataException e)
        {
            System.out.println("Caught: "+e);
        }
    }
}


/*
Output:
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac ApplyAdmission.
java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java ApplyAdmission
Welcome to M.H. Saboo Siddik College of Engineering
...............Application Form.....................
Enter your Name:
Ahlam
Enter your Percentage:
89
Ahlam your Application has been accepted.

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java ApplyAdmission
Welcome to M.H. Saboo Siddik College of Engineering
...............Application Form.....................
Enter your Name:
Ahlam
Enter your Percentage:
61
Sorry Ahlam your Application cannot be accepted.
Caught InvalidDataException

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>

*/

No comments:

Post a Comment