Write a program to demonstrate concept of exception handling.

, by Prashant Gunjal


/*
Aim : Write a program to demonstrate concept of exception handling.
*/
import java.lang.*;
import java.io.*;
class MyException extends Exception
{
MyException(int a)
{
System.out.println("\n\t\tYou r not eligeble because your age is "+a+" (greater)");
}
}

class pr_6
{
static void check(int a)throws MyException
{
if(a>20)
throw new MyException(a);
else
System.out.println("\n\t\tYou are eligible to apply...");
}
public static void main(String args[])throws Exception
{
String ch;
int a;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
do
{
System.out.print("\n\t\tEnter your age : ");
a=Integer.parseInt(br.readLine());
check(a);
System.out.print("\n\t\tDo u want to continue \n\t\t\t1) yes \n\t\t\t2) no \n\t\t\tEnter Choice :   ");
a=Integer.parseInt(br.readLine());
}while(a!=2);
}
catch(MyException e)
{
System.out.println("\t\tCaught Exception "+e);
}
}
}

/* OUTPUT

prashant@prashant-OptiPlex-755:~$ clear

prashant@prashant-OptiPlex-755:~$ javac pr_6.java
prashant@prashant-OptiPlex-755:~$ java pr_6

Enter your age : 12

You are eligible to apply...

Do u want to continue
1) yes
2) no
Enter Choice :   1

Enter your age : 15

You are eligible to apply...

Do u want to continue
1) yes
2) no
Enter Choice :   1

Enter your age : 34

You r not eligeble because your age is 34 (greater)
Caught Exception MyException
prashant@prashant-OptiPlex-755:~$
*/

0 comments: