class AgeException extends Exception
{
private String message;
public AgeException()
{
this.message = "Age Cannot Be Less Than 1";
}
public AgeException(String message)
{
this.message = message;
}
public String toString()
{
return this.message;
}
}
class SetAge
{
private int age;
public void setAge(int age)
{
this.age = age;
}
public void showAge()
{
String message = "Age Cannot Be Greater Than 120";
if(age >=1 && age <= 120)
{
System.out.println("Age Set : " + age);
}
else if ( age > 120)
{
throw new AgeException(message);
}
else
{
throw new AgeException();
}
}
}
public class AgeMain
{
public static void main(String[] args)
{
SetAge setage = null;
try
{
setage = new SetAge();
setage.setAge(-5);
setage.showAge();
}
catch(NullPointerException exception)
{
System.out.println("Exception Object Reference is Null ");
}
catch(AgeException exception )
{
System.out.println("Exceptio : " + exception);
}
}
}
{
private String message;
public AgeException()
{
this.message = "Age Cannot Be Less Than 1";
}
public AgeException(String message)
{
this.message = message;
}
public String toString()
{
return this.message;
}
}
class SetAge
{
private int age;
public void setAge(int age)
{
this.age = age;
}
public void showAge()
{
String message = "Age Cannot Be Greater Than 120";
if(age >=1 && age <= 120)
{
System.out.println("Age Set : " + age);
}
else if ( age > 120)
{
throw new AgeException(message);
}
else
{
throw new AgeException();
}
}
}
public class AgeMain
{
public static void main(String[] args)
{
SetAge setage = null;
try
{
setage = new SetAge();
setage.setAge(-5);
setage.showAge();
}
catch(NullPointerException exception)
{
System.out.println("Exception Object Reference is Null ");
}
catch(AgeException exception )
{
System.out.println("Exceptio : " + exception);
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.