Java Custom Exceptions

JavaViews 1225

Java allows us to create our own exceptions which we call as custom exceptions or user-defined exceptions. In the previous tutorial, we have learned how to use the throw keyword to throw our own exceptions. In this tutorial, we will learn how to create custom exceptions which we require based on the required work-flow.

To understand about the custom exceptions, you should have basic knowledge of java try-catch and java throw keyword.

What is a custom exception

We can create a custom exception class based on the required condition. These classes should extend the Exception class. Using the user-defined class, we can specify the condition for exception along with the required error message.

The need for Custom exceptions

We use custom exceptions mainly for the below reasons:

  • When we want to throw exceptions for a certain condition
  • When we want exceptions specific for business logic or workflow.


Below is the syntax of creating a custom exception class.

class exception_classname extends Exception {
  exception_classname(String s) {

exception_classname – the name of the custom exception class

Exception – parent class

Create a custom exception class

Below are the simple steps to create a custom exception class:

  • Create a java class with a custom exception class name Eg: InSufficientStock (when requested quantity is greater than the available quantity)
  • Extend the custom class with Exception class in the class definition
  • Create a parameterized constructor within the class.
  • Call the parameterized super constructor.


Below is a simple example of a custom exception class creation and how to call the exception from another class. We have created a custom exception class InsufficientStock that extends the Exception class. Within the custom exception class, we create a constructor with a String variable as a parameter and call the super constructor. By doing this, it prints this string value as a message while calling the exception method getMessage(). 

From the actual class Product, we call the custom exception when the condition qty>availableStock is true using the throw keyword. We enclose the caller method using the try-catch block.

class InsufficientStock extends Exception {
  InsufficientStock(String s) {
public class Product {

  int availableStock = 10;
  public void checkProductStock(int qty) throws InsufficientStock {
    if(qty > availableStock)
      throw new InsufficientStock("Insufficient stock available !");
      System.out.println("Product stock is available");
  public static void main(String[] args) {
    Product p = new Product();
    try {
    } catch (InsufficientStock e) {


Insufficient stock available !

Below is another example of creating user-defined exceptions in java without calling the super constructor. Here, instead of calling the superclass constructor, we assign the parameter to the class string variable inside the constructor of the custom exception class. We then create another method toString, to return the value as a string variable when an exception occurs.

Now, when we throw the new exception from the main method, it calls the constructor of the custom exception class and assigns the message to the string the variable. When it catches the exception, the exception object converts to a String value and prints the error message. If we use e.getMessage then it returns null since we are not calling the superclass constructor.

class OwnException extends Exception {
  String message;
  OwnException(String s) {
    message = s;
  public String toString() {
    return ("Exception: " + message);
public class CustomException {

  public static void main(String[] args) {
    try {
      throw new OwnException("My own custom exception");
    catch(Exception e) {


Exception: My own custom exception



Translate »