/**
* Make an Exception class (InvalidInputException) such that 1)Anyone can set
* the message explicitly. 2)With every message, there should be a text like
* "[Name of class]" This Exception will be thrown if user enters the age below
* 1 or above 155 Make another class to use this exception Test this Class and
* observe the output.
*
*
*/
public class InvalidInputException extends Exception {
/**
* constructor with default message
*/
public InvalidInputException() {
super("Invalid Input Exception Generated!!");
}
/**
* constructor with custom message
*
* @param msg
* custom message
*/
public InvalidInputException(String msg) {
super(msg);
}
/**
* execution begin from here
*
* @param args
* commandline arguments
*/
public static void main(String[] args) {
CreateException ce = new CreateException(0);
ce.checkInvalidInputException();
}
}
/**
* Used to check InvalidInputException
*
* @author Ritesh
*
*/
class CreateException {
// age that is the measure
private int age;
/**
* constructor
*
* @param age
* initialization parameter
*/
public CreateException(int age) {
this.age = age;
}
/**
* checks for valid input else throw InvalidInputException
*
*/
public void checkInvalidInputException() {
try {
// checks age range
if (age <> 155) // if not valid age
throw new InvalidInputException(
"In Class : "
+ getClass().getName()
+ " : Invalid age entered : age should be >1 and <155!!");
else { // valid age
System.out.println("Age : " + age);
System.out.println("Valid age Entered!!!");
}
} catch (InvalidInputException iie) {
iie.printStackTrace();
}
}
}
No comments:
Post a Comment