មេរៀនទី៥៣: Custom Exception

ប្រសិនបើ លោកអ្នកកំពុង Exception ផ្ទាល់ ដូចជា custom exception ឬuser-defined exception។
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
super(s);
}
}
class TestCustomException1{
static void validate(int age)throws InvalidAgeException{
if(age<18 br=""> throw new InvalidAgeException(“not valid”);
else
System.out.println(“welcome to vote”);
}
public static void main(String args[]){
try{
validate(13);
}catch(Exception m){System.out.println(“Exception occured: “+m);}
System.out.println(“rest of the code…”);
}
}
Output:Exception occured: InvalidAgeException:not valid
rest of the code…