OneCompiler

Exception handling

47

import java.util.*;
class ageException extends Exception
{
public ageException(String message )
{
super(message );
}
}
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in );
System.out.println("Enter age");
int n=200;
try {
if(n<0 || n>100)
{
throw new ageException("invalid age");
}

System.out.println("valid");
      
  }catch(ageException e){
      System.out.println(e.getMessage());
  }
  finally {
      System.out.println("Thank you");
  }
}

}