Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Custom Exception

If you are creating your own Exception that is known as custom exception or user-defined exception. Java custom exceptions are used to customize the exception according to user need.

By the help of custom exception, you can have your own exception and message.

Let’s see a simple example of java custom exception.

  1. class InvalidAgeException extends Exception{
  2.  InvalidAgeException(String s){
  3.   super(s);
  4.  }
  5. }
  1. class TestCustomException1{
  2.    static void validate(int age)throws InvalidAgeException{
  3.      if(age<18)
  4.       throw new InvalidAgeException(“not valid”);
  5.      else
  6.       System.out.println(“welcome to vote”);
  7.    }
  8.    public static void main(String args[]){
  9.       try{
  10.       validate(13);
  11.       }catch(Exception m){System.out.println(“Exception occured: “+m);}
  12.       System.out.println(“rest of the code…”);
  13.   }
  14. }
Output:Exception occured: InvalidAgeException:not valid
       rest of the code...