首页 > 代码库 > 自定义异常

自定义异常

1 public class AgeBelow18Exception extends RuntimeException {2     /*public String getMessage() {3         return "Please Call Users Above 18 Years";4     }*/5     public AgeBelow18Exception(){6         super("Please Call Users Above 18 Years");7     }8 }

 

 1 package com.intel.aug6; 2  3 import java.util.Scanner; 4  5 class ExceptionTest1 { 6  7     public void takeInput() throws AgeBelow18Exception { 8         Scanner inp = new Scanner(System.in); 9         System.out.println("Enter Your AGE:- ");10         int age = inp.nextInt();11         if (age < 18)12             throw new AgeBelow18Exception();13     }14 15     public static void main(String[] args) {16         ExceptionTest1 et = new ExceptionTest1();17         try {18             et.takeInput();19         } catch (AgeBelow18Exception e) {20             System.out.println(e.getMessage());21         }22     }23 }