Develop User-defined Custom Exception in Java

Previously we learned about exception handling. Now in this post, we will learn how to create our own custom exception in Java? What is the best procedure to create a custom exception, and the Java user-defined custom exception should be extended from which class?

The new class developed by a programmer deriving from either Throwable or any one of its subclass is called custom exception or user-defined exception.

Most of the cases custom exception classes are derived from the Exception class because they must be handled by the programmer when they are thrown.

All exception classes defined by SUN are based on the Java language and API requirements but not for the development of our projects. So, according to the business requirements programmer must develop their own exception class with suitable and meaningful names relevant to the project context.

Procedure to Develop Custom Exception Class

User-defined or custom exception development is a two steps process,

  1. Define a packaged public class deriving from java.lang.Exception class.
  2. Define public no-arg and String parameter constructors with super() call.
    • No-arg constructor for creating an exception object without the message.
    • Parameterized constructor for creating an exception object with the message.
public class ExampleException extends Exception {

   // No-arg constructor with super() call
   public ExampleException (){
     super();
   }

   // String-arg constructor with super() call
   public ExampleException (String msg) {
     super(msg);
   }

}

We can create custom exceptions just by defining a class that is inherited from the Exception class but it is recommended to develop a no-arg and String-arg constructor with super() call.

Java Custom Exception Example

Develop a Java program to add only positive numbers, if the user passes negative numbers then throw your own checked exception NegativeNumberException.

In this Java program, we will create a custom exception NegativeNumberException. Later, we will develop another class Addition. In the Addition class, the add(int a, int b) method throws NegativeNumberException if any of the passing numbers is negative otherwise it returns the sum of numbers. The add() method will be called and the exception will be caught by try/catch block.

// NegativeNumberException.java
public class NegativeNumberException extends Exception {

  // No-arg constructor with super() call
  public NegativeNumberException(){
    super();
  }

  // String-arg constructor with super() call
  public NegativeNumberException(String msg){
    super(msg);
  }

}
// Addition.java
public class Addition {

  public static int add(int a, int b) 
           throws NegativeNumberException {

    if(a<0 || b<0) 
    throw new NegativeNumberException("Pass only +ve number");

    return a+b;

  } 
} 

If any passed number is negative then it will throw NegativeNumberException. No need to write else block, because when if-condition fails then it returns the sum of the passing numbers.

 // Calculator.java
 import java.util.Scanner;

 public class Calculator {

   public static void main(String[] args) {

     // declare variables
     int a = 0, b = 0;

     // Create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter first number: ");
     a = scan.nextInt();
     System.out.print("Enter second number: ");
     b = scan.nextInt();

     // call add method
     try {
       int sum = Addition.add(a, b);
       System.out.println("Sum = " + sum);
     } catch(NegativeNumberException e) {
       System.out.println(e.getMessage());
     }
   }
}

The output of the above program:-

Enter first number: -5
Enter second number: 20
Pass only +ve number

In the next post, we will develop more Java custom exception examples based on real-time.

Note:- We can create our custom exception derived from anyone of Error, RuntimeException, Throwable, Exception, or one of its sub-class. But generally, we develop java user-defined custom exceptions for the business logic so it should be derived from the Exception class.

A Custom Exception should be Derived From Which Class & Why?

Why should we extend the custom exception class from the java.lang.Exception class only?

Answer:- For making sure, it is compulsory handling by the programmer, it is recommended to derive custom exception from Exception class only.

What happens if we extend our custom exception class from java.lang.Error, java.lang.Throwable, or java.lang.RuntimeException?

The java.lang.Error class is given for the exceptions which are raised due to the JVM internal logic. Because we are not writing our exception for handling errors in JVM internal logic, so we should not write exception classes deriving from java.lang.Error class.

SUN Microsystem created only two types of exception java.lang.Error and java.lang.Exception. If we are creating our custom exception from java.lang.Throwable class, then it creates a new category of exception. It is the wrong design, and it is not recommended to create a new category of exceptions because this custom exception will not be caught by the catch(Exception e) block.

The exceptions derived from java.lang.RuntimeException class will be an unchecked exception, handling of those exceptions are doesn’t check by the compiler. Hence, we must extend our exception from java.lang.RuntimeException class only if we don’t want to validate these exceptions handling by the compiler. Generally, we write exceptions for the business logic, and handling of those exceptions must be checked by the compiler otherwise program execution is terminated abnormally. So, it is not recommended to deriving the custom exception from the RuntimeException class.

We should create a custom exception class for throwing these exceptions when a condition is failed and also for forcing the compiler to validate or to check whether the exception is handled. So, it is recommended to derive the exception from java.lang.Exception class. The exceptions derived from the Exception class are checked exception and handling of those exceptions checked by the compiler.

Developing User-Defined Unchecked Exception

Q) Can we develop checked exceptions?
Yes, we can develop checked exceptions derived from java.lang.Exception class.

Q) Can we develop unchecked exceptions?
Yes, we can develop unchecked exceptions derived from java.lang.RuntimeException class.

Many people think that we can’t develop a java custom exception as an unchecked exception. But it is wrong, we can develop unchecked exceptions.

Note:- In real-time application development, It is not recommended to develop the unchecked exception. The program is given only for knowledge and practice purposes.

Here we will develop the previous exception NegativeNumberException as an unchecked exception. To develop unchecked exceptions, our custom exception should be derived from java.lang.RuntimeException class.

public class NegativeNumberException extends RuntimeException {

   public NegativeNumberException(){super();}

   public NegativeNumberException(String msg){
      super(msg);
   }

}
public class Addition {

  public static int add(int a, int b) 
          throws NegativeNumberException {

      if(a<0 || b<0) throw 
          new NegativeNumberException("Pass only +ve number");

      return a+b;
    }

 }
//Without catching the exception
public class Calculator {

  public static void main(String[] args) {

    // Catching the Exception is optional
    // Compiler will not give error
    int sum = Addition.add(-5,7);
    System.out.println("Sum = " + sum);
  }

}

Output:-

Exception in thread “main” NegativeNumberException: Pass only +ve number
at Addition.add(Addition.java:5)
at Calculator.main(Calculator.java:7)

In this program the NegativeNumberException is inherited from RuntimeException class so, it is an unchecked exception i.e. compiler doesn’t verify the exception handling. Hence, we can write a program without throwing or handling the exception.

//Catching exception using try-catch block
public class Calculator {

   public static void main(String[] args){

     // Catching the Exception is optional
     try {
       int sum = Addition.add(-5,7);
       System.out.println("Sum = " + sum);
     } catch(NegativeNumberException e) {
       System.out.println(e.getMessage());
     }

   }
 }

Output:-

Pass only +ve number

Q) Can we develop partial checked exceptions?
No, we can’t create partial checked exceptions. For partial checked exceptions our custom exception should have both checked and unchecked exceptions, which is not possible.

Learn more:- Unchecked Exceptions — The Controversy

Related Java topics

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *