java user defined exception example

Solutions on MaxInterview for java user defined exception example by the best coders in the world

showing results for - "java user defined exception example"
Salim
15 Sep 2018
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  }  
2223
24Output:Exception occured: InvalidAgeException:not valid
25       rest of the code...