Customized Exception

Customized Exception

Based on our programming requirement some times we have to create our own exception, which are nothing but β€œCustomized Exception”.

E.g

 TooYoungException.
 TooOldException.
 InSufficiantFundsTransfer. 
public class TooYoungException extends RuntimeException {
   TooYoungException(String s) {
      super(s);
   }
}
public class TooOldException extends RuntimeException {
   TooOldException(String s) {
      super(s);
   }
}
public class CustomExceptionDemo {
   public static void main(String arg[]) {
      int age = Integer.parseInt(arg[0]);
      if(age > 60) {
         throw new TooOldException("Younger age is already over");
      } else if(age < 18)
         throw new TooYoungException("Please wait same more time");
      }
      System.out.println("Thanks for register");
   }
}

Note

It is recommended to define customized exceptions as unchecked. i.e our custom exceptions class should extends R.E either directly or indirectly.

Customized Exception


Scroll to top