---
title: "How to create a custom exception in Java, and when would to use it?"  
description: "How to create a custom exception in Java, and when would to use it?"  
author: "Steilla Mitchel"  
published: 2023-07-23  
updated: 2023-07-24  
canonical: https://www.mindstick.com/forum/159237/how-to-create-a-custom-exception-in-java-and-when-would-to-use-it  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 2 minutes  

---

# How to create a custom exception in Java, and when would to use it?

How to create a [custom exception](https://www.mindstick.com/forum/156754/how-to-create-custom-exception-c-sharp) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming), and when would to use it?

## Replies

### Reply by Aryan Kumar

A [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) is an exception that you create yourself. To create a custom exception, you need to create a class that extends the Exception class.

Here is an example of how to create a custom exception:

```plaintext
public class MyException extends Exception {

  private String message;

  public MyException(String message) {
    this.message = message;
  }

  public String getMessage() {
    return this.message;
  }
}
```

This class defines a custom exception called MyException. The MyException class extends the Exception class, and it has a single property called message. The message property is a string that contains the message that will be displayed when the exception is thrown.

To throw a custom exception, you need to create an object of the custom exception class and then throw it using the throw keyword.

Here is an example of how to throw a custom exception:

```plaintext
public class ThrowCustomExceptionExample {

  public static void main(String[] args) {
    try {
      // Code that may throw an exception
    } catch (MyException e) {
      System.out.println("An error occurred: " + e.getMessage());
    } finally {
      System.out.println("This code will always execute");
    }
  }
}
```

In this example, the try block contains the code that may throw an MyException. If the MyException is thrown, it will be caught by the catch block. The catch block then prints a message to the console indicating that an error occurred.

The message property of the MyException object is set to the string "This is an error message". This message will be displayed when the exception is thrown.

The finally block is executed regardless of whether or not an exception is thrown. In this example, the finally block prints the message "This code will always execute" to the console.

Here are some reasons why you might want to use a custom exception:

- To provide more information about the error that occurred.
- To provide a way to handle the error differently than a built-in exception.
- To create a custom exception hierarchy.

Here are some additional things to keep in mind about custom exceptions:

- Custom exceptions must extend the Exception class.
- Custom exceptions can have constructors that take arguments.
- Custom exceptions can have methods.
- Custom exceptions can be thrown and caught just like built-in exceptions.


---

Original Source: https://www.mindstick.com/forum/159237/how-to-create-a-custom-exception-in-java-and-when-would-to-use-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
