Q:- Java program to create custom exception that throw exception if age entered is less than 18.
Anonymous
import java.util.Scanner; class MaxAge extends Exception { public MaxAge(String str) { System.out.println(str); } } public class CustomAgeExeption { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("enter age: "); int age=Integer.parseInt(s.nextLine()); if(age<18) { try { throw new MaxAge("Invalid age"); } catch(MaxAge a) { System.out.println(a.getClass()); } } else { System.out.println("valid age"); } } }
Check out your Company Bowl for anonymous work chats.