Tuesday, February 17, 2015

Custom Exception

public class InvalidAgeException extends Exception {
InvalidAgeException(String s) {
super(s); //calling super class constructor
}
}


class TestCustomException {

static void validate(int age) throws InvalidAgeException , ArithmeticException{
if (age < 18)
throw new InvalidAgeException("Age limit is not satisfied");
else
System.out.println("welcome to vote");
}

public static void main(String args[]) {
try {
validate(13);
} catch (Exception exp) {
System.out.println("Exception occured: " + exp);
}

System.out.println("rest of the code...");
}
}

No comments:

Post a Comment