Custom Exceptions in Java
Java allows you to define your own exception classes by extending Exception (for checked exceptions) or RuntimeException (for unchecked exceptions). Custom...
Creating Your Own Exception Types
Java allows you to define your own exception classes by extending Exception (for checked exceptions) or RuntimeException (for unchecked exceptions). Custom exceptions give domain-specific meaning to error conditions. Instead of throwing a generic IllegalArgumentException, a payment system can throw InsufficientFundsException with fields for the attempted amount and the available balance. Callers know exactly what went wrong without parsing a generic error message.
When to Create a Custom Exception Create a custom exception when: the existing exception types do not accurately describe the error; you need to carry domain-specific data alongside the error (account number, transaction ID); callers need to distinguish this error from other errors programmatically; or you want a consistent exception hierarchy for a library or module. Avoid creating custom exceptions just because you can—unnecessary exception classes add maintenance burden without benefit.
Designing a Good Custom Exception A well-designed custom exception should extend the most specific appropriate superclass. It should provide at least a constructor that accepts a message string and another that accepts a message plus a cause. Always chain causes when wrapping lower-level exceptions: this.cause = lowerLevelException. Include relevant domain fields with accessor methods so callers can retrieve structured information without parsing the message string, which could change across versions.
Checked vs Unchecked Custom Exceptions Extending Exception creates a checked exception that callers must handle or declare. This is appropriate for conditions callers can reasonably recover from—like a user entering an incorrect password, where the caller can prompt again. Extending RuntimeException creates an unchecked exception suitable for conditions indicating programming errors or unrecoverable states. Most modern Java frameworks favor unchecked custom exceptions to avoid cluttering method signatures with exception declarations.
Code Example: class InsufficientFundsException extends Exception { private double available; private double attempted;
InsufficientFundsException(double available, double attempted) { super(String.format("Cannot withdraw %.2f; only %.2f available.", attempted, available)); this.available = available; this.attempted = attempted; }
double getAvailable() { return available; } double getAttempted() { return attempted; } }
// Usage void withdraw(double amount) throws InsufficientFundsException { if (amount > balance) throw new InsufficientFundsException(balance, amount); balance -= amount; }
Interview Tip: Interviewers test whether you know to always include a constructor that accepts a Throwable cause: public MyException(String message, Throwable cause) { super(message, cause); }. Without this constructor, exception chaining is impossible, meaning lower-level root causes are permanently lost when you wrap them. Good exception design preserves the full stack of causes for accurate diagnosis.