User Defined Exception in Java with Example

Introduction

User defined exception in Java are created by users.

In Java, we can create our own exception classes. Those exceptions are known as User defined or custom exception.

These self created exceptions can be throw using java ‘throw‘ keyword.

Let’s learn how to create userdefined or custom exception in detail :

What is User Defined Exception?

Java gives us the facility to create our own Exception classes.

Suppose, we have some logic in our application that check for age_limit , we want check of the condition that age > 18.

In this case, we can create user defined exception such as AgeLimitException.

So AgeLimitException is our user defined exception in Java.

Now let’s jump on How part :

How to Create User Defined Exception in Java?

We can create user defined or custom exception in Java by extending Exception class.

All user defined exceptions are thrown by using throw keyword in Java.

These are similar to pre defined exception in Java. Some predefined exceptions in Java are : NullPointerException, ArithmeticException etc.

Below are the steps to create user defined or custom exception :

  1. create a class for your user defined exception.
  2. extend the Exception class.
  3. create parameterized constructor having String as parameter.
  4. call super(<string parameter>) passing the string parameter.

Let’s see example :

Example of User Defined Exception in Java

AgeLimitException.java :

class AgeLimitException extends Exception{  
    
 AgeLimitException(String s){  
  super(s);  
 
 }  
}  

Test.java :

public class Test{  
  
   static void checkAge(int age)throws AgeLimitException{  
       
     if(age<18) { 
      throw new AgeLimitException("Age is less than 18");  
     }
     else  {
      System.out.println("Age Test Passed");  
     }
      
   }  
     
   public static void main(String args[]){  
       
      try{  
          
      checkAge(16);  
      
      }
      
      catch(Exception e){
          
          System.out.println("Exception -- "+e);
          
          
      }  
  
      System.out.println("Rest of the Logic....");  
  }  

    
}  

Output:

 Exception -- AgeLimitException: Age is less than 18 

 Rest of the Logic.... 

Explanation:

In our Exception class, we have passed string parameter in super(s). It is because we want that string literal to be the exception message.

throw keyword is used to throw the exception.

How to create Unchecked Exception in Java?

The above process is to create custom checked exception in Java. Now let’s see how to create unchecked exception in java :

  • create a class for your user defined unchecked exception.
  • extend the RuntimeException class.
  • create parameterized constructor having String as parameter.
  • call super(<string parameter>) passing the string parameter.

Example of Custom Unchecked Exception in Java

public class CustomException extends RuntimeException {
    
   public CustomException(String s) {
      super(s);
      
   }
}

Further Readings:

Conclusion

User defined exceptions in java are created manually by users just like some predefined java exceptions. We create these exceptions to fulfil program logic.

We have learn how to create checked exception in Java with Examples.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply