User-defined custom exception in Java

User-defined custom exception in Java

In Java, exceptions are used to indicate that an abnormal condition has occurred. While Java provides a rich set of built-in exceptions, there are situations where you might want to define your own exceptions to capture application-specific issues more accurately. Such exceptions are called user-defined or custom exceptions.

This tutorial will guide you through creating and using a custom exception in Java.

Step 1: Create the Custom Exception Class

To create a custom exception:

  1. Define a new class.
  2. Typically, this class should extend the Exception class. If you want your exception to be an unchecked exception (not requiring a catch block or a throws declaration), extend RuntimeException instead.
public class CustomException extends Exception { // You can provide various constructors // Default constructor public CustomException() { super(); } // Constructor that accepts a message public CustomException(String message) { super(message); } // Constructor that accepts a message and a cause public CustomException(String message, Throwable cause) { super(message, cause); } // ... You can add more constructors if needed } 

Step 2: Use the Custom Exception

To use the custom exception, you throw it in situations where the exception condition arises, just like you'd do with built-in exceptions.

Here's an example of a simple method that throws our CustomException if a provided number is negative:

public class NumberProcessor { public void processNumber(int number) throws CustomException { if (number < 0) { throw new CustomException("Negative numbers are not allowed!"); } // ... process the number System.out.println("Processing number: " + number); } } 

Step 3: Handle the Custom Exception

When using a method that throws a checked exception (like our CustomException), you'll need to either catch it or declare it with throws in the calling method.

Here's an example of handling our custom exception:

public class App { public static void main(String[] args) { NumberProcessor processor = new NumberProcessor(); try { processor.processNumber(-5); } catch (CustomException e) { System.out.println("Caught exception: " + e.getMessage()); } } } 

In this example, if you run the main method, you'll see the output: "Caught exception: Negative numbers are not allowed!"

Conclusion

Custom exceptions provide a way to handle domain-specific error scenarios in a more meaningful way than using generic exceptions. They can make your code more readable and maintainable, as they give a clearer indication of the types of errors your application might encounter. Just remember to use them judiciously; don't create custom exceptions if the standard Java exceptions can appropriately represent the error condition.


More Tags

mysql-error-1052 max-path reportviewer conflict asp.net-mvc-partialview labview java-7 smartcard theano mysql-error-1222

More Programming Guides

Other Guides

More Programming Examples