Custom Exception Example in java


1: class TooYoungException extends RuntimeException {

2:

3: TooYoungException(String s) {

4: super(s);

5: }

6: }

7:

8: class TooOldException extends RuntimeException {

9:

10: TooOldException(String s) {

11: super(s);

12: }

13: }

14:

15: class CustomExceptionDemo {

16: public static void main(String[] args) {

17: int age = Integer.parseInt(args[0]);

18: if (age > 60) {

19: throw new TooYoungException(

20: “please wait some more time,we will contact you with mail”);

21:

22: } else if (age < 18) {

23: throw new TooOldException(“you crassed your age limit”);

24: } else {

25: System.out.println(“you will get best choice matches soon”);

26: }

27: }

28: }

OUTPUT :

image