Java tutorials > Core Java Fundamentals > Exception Handling > How to throw custom exceptions?
How to throw custom exceptions?
This tutorial demonstrates how to create and throw custom exceptions in Java. Custom exceptions allow you to handle specific error scenarios in your application more effectively by providing tailored exception types that encapsulate relevant information.
Creating a Custom Exception
To create a custom exception, you need to extend the The constructors allow you to create the exception with a custom message and/or a cause (another exception that led to this one).Exception
class (for checked exceptions) or the RuntimeException
class (for unchecked exceptions). The example above extends Exception
, making it a checked exception. This means that any method that throws CustomException
must either handle it with a try-catch
block or declare it in its throws
clause.
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}
Throwing a Custom Exception
This example demonstrates how to throw a custom exception. The Note the processData
method checks if the input data
is negative. If it is, it throws a new CustomException
with a descriptive message. The main
method calls processData
within a try-catch
block to handle the exception if it's thrown.throws CustomException
clause in the processData
method signature. This is required because CustomException
is a checked exception.
public class Example {
public void processData(int data) throws CustomException {
if (data < 0) {
throw new CustomException("Data cannot be negative: " + data);
}
// Process data if it's valid
System.out.println("Processing data: " + data);
}
public static void main(String[] args) {
Example example = new Example();
try {
example.processData(-5);
} catch (CustomException e) {
System.err.println("Caught CustomException: " + e.getMessage());
}
}
}
Concepts behind the Snippet
The core concept is to create specialized exception classes that represent specific error conditions within your application. This allows for more precise error handling and better communication of error details. By extending Exception
or RuntimeException
, you inherit the standard exception handling behavior of Java. The ability to pass a message and a cause provides context for the exception, aiding in debugging and error reporting.
Real-Life Use Case
Imagine you are developing an e-commerce application. You might create custom exceptions like InsufficientStockException
when a customer tries to order more items than are available, or InvalidPaymentDetailsException
when the provided credit card information is incorrect. These custom exceptions allow you to handle these specific scenarios gracefully, perhaps by displaying a user-friendly error message or initiating a rollback of the transaction.
Best Practices
DataValidationException
, ResourceNotFoundException
).
Interview Tip
When discussing custom exceptions in an interview, be prepared to explain the difference between checked and unchecked exceptions, and why you might choose one over the other. Also, be ready to provide examples of situations where custom exceptions are particularly useful, and explain how they improve error handling in complex applications. Illustrate a real use-case of custom exception with your previous project.
When to use them
Use custom exceptions when:
Memory footprint
Custom exceptions, like all Java objects, consume memory. The memory footprint depends on the fields within the custom exception class. A simple exception with just a message has a relatively small memory footprint. However, if you include large data structures or complex objects within your custom exception, the memory footprint will increase. Consider carefully what data you need to include in the exception and avoid unnecessary fields.
Alternatives
Alternatives to throwing custom exceptions include: However, these alternatives often lead to less clear and more verbose code, especially when dealing with complex error scenarios. Custom exceptions provide a more structured and expressive way to handle errors.
Pros
Cons
FAQ
-
What is the difference between a checked and an unchecked exception?
A checked exception must be either caught or declared in the
throws
clause of a method. The compiler enforces this requirement. Checked exceptions are typically used for conditions that the caller might reasonably be expected to recover from. An unchecked exception (extendsRuntimeException
) does not need to be caught or declared. These are usually indicative of programming errors and are not generally recoverable. -
When should I use a checked exception versus an unchecked exception?
Use checked exceptions for errors that the calling code can reasonably be expected to handle or recover from. Use unchecked exceptions for errors that are the result of programming mistakes or conditions that are generally unrecoverable, such as
NullPointerException
orIllegalArgumentException
. -
Can I create a custom unchecked exception?
Yes, you can create a custom unchecked exception by extending the
RuntimeException
class or one of its subclasses. -
Why should I add a constructor to my custom Exception class?
Adding constructors to your custom exception class allows you to initialize the exception with a specific message and/or a cause. This provides more context about the error and helps in debugging. The commonly used constructors include a constructor that takes only a message string and a constructor that takes both a message and a cause (another throwable that led to the current exception).