employer cover photo
employer logo
employer logo

Maxgen Technologies

Is this your company?

Maxgen Technologies interview question

Q:- Java program to create custom exception that throw exception if age entered is less than 18.

Interview Answer

Anonymous

10 Feb 2021

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"); } } }