Monday, March 24, 2014

Exception handling in java

import java.io.*;
class MyException extends Exception
{
MyException(String Message)
{
super(Message);
}
}
class ex
{
String name;
int age;
public void input()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name:");
name=br.readLine();
System.out.println("Enter age:");
age=Integer.parseInt(br.readLine());
if(age<0)
{
throw new MyException("Invalid age/Age is negative");
}
}
catch(Exception e)
{
System.out.println("caught exception");
System.out.println(e.getMessage());
}
}
public static void main(String args[])
{
ex e=new ex();
e.input();
}
}

http://adf.ly/hFlmI

No comments:

Post a Comment