1class InvalidAgeException extends Exception{
2 InvalidAgeException(String s){
3 super(s);
4 }
5}
6class TestCustomException1{
7
8 static void validate(int age)throws InvalidAgeException{
9 if(age<18)
10 throw new InvalidAgeException("not valid");
11 else
12 System.out.println("welcome to vote");
13 }
14
15 public static void main(String args[]){
16 try{
17 validate(13);
18 }catch(Exception m){System.out.println("Exception occured: "+m);}
19
20 System.out.println("rest of the code...");
21 }
22}
23
24Output:Exception occured: InvalidAgeException:not valid
25 rest of the code...