Java Custom Exceptions
Java Custom Exceptions
A custom exception is a class you write that extends Exception or RuntimeException to represent a failure specific to your application's domain. InsufficientStockException, InvalidOrderStateException, PaymentDeclinedException — none of these exist in the JDK, because the JDK has no idea what an "order" or a "payment" is in your system. Custom exceptions are how Java's generic exception mechanism becomes specific to the problem you are actually solving. Used well, they make error handling self-documenting. Used poorly, they become an unmanageable pile of near-identical classes that add ceremony without adding meaning.
What Is a Custom Exception?
A custom exception is any class that extends Throwable — in practice, almost always Exception (for checked) or RuntimeException (for unchecked), since extending Throwable or Error directly is reserved for JVM-level conditions. The class inherits all of Throwable's behaviour — message, cause, stack trace — and adds whatever domain-specific fields and methods make sense for the failure it represents.
MINIMAL CUSTOM EXCEPTION:
public class InsufficientStockException extends RuntimeException {
public InsufficientStockException(String message) {
super(message);
}
}
WHAT YOU GET FOR FREE BY EXTENDING Exception OR RuntimeException:
getMessage() — the message passed to the constructor
getCause() — the wrapped exception, if any
getStackTrace() — array of stack frame elements
printStackTrace() — prints type, message, and full trace
addSuppressed() — for try-with-resources suppression
WHAT YOU TYPICALLY ADD:
Domain-specific fields: the order ID, the requested quantity, the limit
A constructor that takes those fields and builds a clear message from them
Getters for those fields, so catching code can react programmatically
(not just read a string message)
DECISION 1 — Exception or RuntimeException?
extends Exception → CHECKED — callers must catch or declare
extends RuntimeException → UNCHECKED — no compiler enforcement
(covered in depth in the Checked vs Unchecked Exceptions article —
this article assumes that decision is already made and focuses on
HOW to design the exception class itself)
DECISION 2 — Flat hierarchy or exception family?
FLAT: each custom exception extends Exception/RuntimeException directly
FAMILY: a common abstract base (e.g., OrderException) that all
domain-specific exceptions extend — enables catching the
whole family with one catch block
Basic Overview — The Anatomy of a Well-Designed Custom Exception
public class InsufficientStockException extends RuntimeException {
private final String productId; <- domain field 1: WHAT failed
private final int requestedQty; <- domain field 2: the input that caused it
private final int availableQty; <- domain field 3: the actual constraint
public InsufficientStockException(
String productId, int requestedQty, int availableQty) {
super(String.format( <- message built FROM the fields
"Insufficient stock for %s: requested %d, available %d",
productId, requestedQty, availableQty));
this.productId = productId;
this.requestedQty = requestedQty;
this.availableQty = availableQty;
}
public String getProductId() { return productId; } <- getters let
public int getRequestedQty() { return requestedQty; } <- catching code
public int getAvailableQty() { return availableQty; } <- react programmatically
}
WHY EACH PART MATTERS:
Fields: catching code can do "if (e.getAvailableQty() == 0)" —
a string message cannot be safely parsed for this
Constructor: builds the message ONCE, consistently, from the fields —
every throw site produces an identically-formatted message
Getters: expose the fields without exposing mutability —
no setters; exceptions should be immutable after construction
Extending Exception vs RuntimeException
The first decision when writing a custom exception is which class to extend. This determines whether the compiler enforces handling — and it should be a deliberate choice based on whether callers have a genuine recovery path, not a default.
1// File: CheckedVsUncheckedCustomDemo.java
2
3public class CheckedVsUncheckedCustomDemo {
4
5 // CHECKED custom exception — extends Exception
6 // Use when: callers have a REAL recovery strategy and should be
7 // forced to acknowledge this failure mode
8 static class SeatNotAvailableException extends Exception {
9 private final String seatNumber;
10
11 SeatNotAvailableException(String seatNumber) {
12 super("Seat not available: " + seatNumber);
13 this.seatNumber = seatNumber;
14 }
15
16 String getSeatNumber() { return seatNumber; }
17 }
18
19 // UNCHECKED custom exception — extends RuntimeException
20 // Use when: the failure is a programming error or system-level problem
21 // that should propagate to a single boundary handler
22 static class BookingSystemException extends RuntimeException {
23 BookingSystemException(String message, Throwable cause) {
24 super(message, cause);
25 }
26 }
27
28 // Method declares throws for the CHECKED exception only
29 static String bookSeat(String seatNumber, boolean seatTaken, boolean systemDown)
30 throws SeatNotAvailableException {
31
32 if (systemDown) {
33 // Unchecked — no throws needed, propagates to global handler
34 throw new BookingSystemException(
35 "Booking system unavailable", new RuntimeException("DB connection refused"));
36 }
37 if (seatTaken) {
38 // Checked — REQUIRES throws on this method
39 throw new SeatNotAvailableException(seatNumber);
40 }
41 return "Booked: " + seatNumber;
42 }
43
44 public static void main(String[] args) {
45
46 System.out.println("=== Checked custom exception — caller has a recovery path ===");
47 try {
48 System.out.println(bookSeat("A12", false, false));
49 System.out.println(bookSeat("B07", true, false)); // seat taken
50 } catch (SeatNotAvailableException snae) {
51 // Recovery: suggest an alternative seat
52 System.out.println("Seat " + snae.getSeatNumber() +
53 " unavailable — suggesting alternative seats nearby");
54 }
55
56 System.out.println();
57
58 System.out.println("=== Unchecked custom exception — system failure, no recovery ===");
59 try {
60 bookSeat("C03", false, true); // system down
61 } catch (BookingSystemException bse) {
62 System.out.println("System error: " + bse.getMessage());
63 System.out.println("Root cause: " + bse.getCause().getMessage());
64 } catch (SeatNotAvailableException snae) {
65 System.out.println("Seat unavailable: " + snae.getSeatNumber());
66 }
67 }
68}Output:
=== Checked custom exception — caller has a recovery path ===
Booked: A12
Seat B07 unavailable — suggesting alternative seats nearby
=== Unchecked custom exception — system failure, no recovery ===
System error: Booking system unavailable
Root cause: DB connection refused
Constructors — Following the Throwable Convention
Throwable provides four standard constructors. A well-designed custom exception typically implements all four — even if your code only ever uses one or two — because libraries, frameworks, and other developers calling your exception class expect this convention to be available.
1// File: ExceptionConstructorsDemo.java
2
3public class ExceptionConstructorsDemo {
4
5 // Implementing all four standard Throwable-style constructors
6 static class ServiceException extends RuntimeException {
7
8 // 1. No-argument constructor — rarely used directly, but completes the contract
9 public ServiceException() {
10 super();
11 }
12
13 // 2. Message only — the most commonly used form
14 public ServiceException(String message) {
15 super(message);
16 }
17
18 // 3. Message and cause — used when wrapping another exception
19 public ServiceException(String message, Throwable cause) {
20 super(message, cause);
21 }
22
23 // 4. Cause only — message is derived from the cause's toString()
24 public ServiceException(Throwable cause) {
25 super(cause);
26 }
27 }
28
29 // Demonstrating when each constructor form is the right choice
30 static void demonstrateConstructorUsage() {
31
32 // Form 2 — message only: a NEW failure detected here, no underlying cause
33 try {
34 throw new ServiceException("Order total cannot exceed Rs.50,000 per transaction");
35 } catch (ServiceException se) {
36 System.out.println("Form 2 [message only]:");
37 System.out.println(" Message: " + se.getMessage());
38 System.out.println(" Cause: " + se.getCause());
39 }
40
41 System.out.println();
42
43 // Form 3 — message + cause: WRAPPING an underlying failure with domain context
44 try {
45 try {
46 throw new java.io.IOException("Connection reset");
47 } catch (java.io.IOException ioException) {
48 throw new ServiceException("Failed to process payment", ioException);
49 }
50 } catch (ServiceException se) {
51 System.out.println("Form 3 [message + cause]:");
52 System.out.println(" Message: " + se.getMessage());
53 System.out.println(" Cause: " + se.getCause());
54 }
55
56 System.out.println();
57
58 // Form 4 — cause only: less common, message defaults to cause.toString()
59 try {
60 try {
61 throw new NumberFormatException("For input string: \"abc\"");
62 } catch (NumberFormatException nfe) {
63 throw new ServiceException(nfe); // no custom message — uses cause's toString()
64 }
65 } catch (ServiceException se) {
66 System.out.println("Form 4 [cause only]:");
67 System.out.println(" Message: " + se.getMessage()); // derived from cause
68 System.out.println(" Cause: " + se.getCause());
69 }
70 }
71
72 public static void main(String[] args) {
73 demonstrateConstructorUsage();
74 }
75}Output:
Form 2 [message only]:
Message: Order total cannot exceed Rs.50,000 per transaction
Cause: null
Form 3 [message + cause]:
Message: Failed to process payment
Cause: java.io.IOException: Connection reset
Form 4 [cause only]:
Message: java.lang.NumberFormatException: For input string: "abc"
Cause: java.lang.NumberFormatException: For input string: "abc"
Domain Fields and Error Codes
The single most valuable addition a custom exception can make over a generic RuntimeException with a string message is structured data: fields that catching code can read programmatically, without parsing a message string.
1// File: DomainFieldsDemo.java
2
3public class DomainFieldsDemo {
4
5 // Custom exception with structured domain fields AND an error code
6 // The error code is what API responses, logs, and monitoring dashboards
7 // actually key on — the message is for humans, the code is for systems
8 static class PaymentDeclinedException extends RuntimeException {
9
10 public enum DeclineReason {
11 INSUFFICIENT_FUNDS, CARD_EXPIRED, FRAUD_SUSPECTED, BANK_UNAVAILABLE
12 }
13
14 private final String transactionId;
15 private final double amount;
16 private final DeclineReason reason;
17
18 public PaymentDeclinedException(
19 String transactionId, double amount, DeclineReason reason) {
20 super(String.format("Payment declined for transaction %s (Rs.%.2f): %s",
21 transactionId, amount, reason));
22 this.transactionId = transactionId;
23 this.amount = amount;
24 this.reason = reason;
25 }
26
27 public String getTransactionId() { return transactionId; }
28 public double getAmount() { return amount; }
29 public DeclineReason getReason() { return reason; }
30
31 // Convenience method — catching code can check WITHOUT string comparison
32 public boolean isRetryable() {
33 return reason == DeclineReason.BANK_UNAVAILABLE;
34 }
35 }
36
37 static void chargeCard(String transactionId, double amount, String cardStatus) {
38 if (cardStatus.equals("EXPIRED")) {
39 throw new PaymentDeclinedException(
40 transactionId, amount, PaymentDeclinedException.DeclineReason.CARD_EXPIRED);
41 }
42 if (cardStatus.equals("LOW_BALANCE")) {
43 throw new PaymentDeclinedException(
44 transactionId, amount, PaymentDeclinedException.DeclineReason.INSUFFICIENT_FUNDS);
45 }
46 if (cardStatus.equals("BANK_DOWN")) {
47 throw new PaymentDeclinedException(
48 transactionId, amount, PaymentDeclinedException.DeclineReason.BANK_UNAVAILABLE);
49 }
50 System.out.println(" Charged Rs." + amount + " for " + transactionId);
51 }
52
53 public static void main(String[] args) {
54
55 System.out.println("=== Successful charge ===");
56 chargeCard("TXN-001", 999.0, "VALID");
57
58 System.out.println();
59
60 System.out.println("=== Declined: different reasons drive different responses ===");
61 String[][] cases = {
62 {"TXN-002", "1499.0", "EXPIRED"},
63 {"TXN-003", "299.0", "LOW_BALANCE"},
64 {"TXN-004", "5000.0", "BANK_DOWN"}
65 };
66
67 for (String[] testCase : cases) {
68 try {
69 chargeCard(testCase[0], Double.parseDouble(testCase[1]), testCase[2]);
70 } catch (PaymentDeclinedException pde) {
71 System.out.println(" " + pde.getMessage());
72
73 // Reading STRUCTURED fields, not parsing the message string
74 switch (pde.getReason()) {
75 case CARD_EXPIRED ->
76 System.out.println(" Action: prompt user to update card");
77 case INSUFFICIENT_FUNDS ->
78 System.out.println(" Action: suggest alternative payment method");
79 case BANK_UNAVAILABLE ->
80 System.out.println(" Action: retryable=" + pde.isRetryable() +
81 " — queue for automatic retry");
82 case FRAUD_SUSPECTED ->
83 System.out.println(" Action: flag for manual review");
84 }
85 }
86 }
87 }
88}Output:
=== Successful charge ===
Charged Rs.999.0 for TXN-001
=== Declined: different reasons drive different responses ===
Payment declined for transaction TXN-002 (Rs.1499.00): CARD_EXPIRED
Action: prompt user to update card
Payment declined for transaction TXN-003 (Rs.299.00): INSUFFICIENT_FUNDS
Action: suggest alternative payment method
Payment declined for transaction TXN-004 (Rs.5000.00): BANK_UNAVAILABLE
Action: retryable=true — queue for automatic retry
Building an Exception Hierarchy
When an application has many related custom exceptions, a common abstract base class lets callers catch the entire family with one catch block when they do not need to distinguish between specific failures, while still allowing specific catches when they do.
1// File: ExceptionHierarchyDemo.java
2
3import java.util.List;
4
5public class ExceptionHierarchyDemo {
6
7 // Abstract base — never thrown directly, only extended
8 // Carries fields common to ALL order-related failures
9 static abstract class OrderException extends RuntimeException {
10 private final String orderId;
11
12 protected OrderException(String orderId, String message) {
13 super(message);
14 this.orderId = orderId;
15 }
16
17 public String getOrderId() { return orderId; }
18 }
19
20 // Specific exceptions — each extends the common base
21 static class OrderNotFoundException extends OrderException {
22 OrderNotFoundException(String orderId) {
23 super(orderId, "Order not found: " + orderId);
24 }
25 }
26
27 static class OrderAlreadyShippedException extends OrderException {
28 private final String shippedDate;
29
30 OrderAlreadyShippedException(String orderId, String shippedDate) {
31 super(orderId, "Order " + orderId + " already shipped on " + shippedDate +
32 " — cannot modify");
33 this.shippedDate = shippedDate;
34 }
35
36 String getShippedDate() { return shippedDate; }
37 }
38
39 static class OrderCancellationWindowExpiredException extends OrderException {
40 private final int hoursElapsed;
41
42 OrderCancellationWindowExpiredException(String orderId, int hoursElapsed) {
43 super(orderId, "Order " + orderId + " cancellation window expired (" +
44 hoursElapsed + "h elapsed, limit is 24h)");
45 this.hoursElapsed = hoursElapsed;
46 }
47
48 int getHoursElapsed() { return hoursElapsed; }
49 }
50
51 static void cancelOrder(String orderId, String status, int hoursElapsed) {
52 if (status.equals("NOT_FOUND")) {
53 throw new OrderNotFoundException(orderId);
54 }
55 if (status.equals("SHIPPED")) {
56 throw new OrderAlreadyShippedException(orderId, "2026-06-10");
57 }
58 if (hoursElapsed > 24) {
59 throw new OrderCancellationWindowExpiredException(orderId, hoursElapsed);
60 }
61 System.out.println(" Cancelled: " + orderId);
62 }
63
64 public static void main(String[] args) {
65
66 System.out.println("=== Catching the FAMILY with one block (generic handling) ===");
67 List<String[]> requests = List.of(
68 new String[]{"ORD-001", "PENDING", "5"},
69 new String[]{"ORD-002", "NOT_FOUND", "0"},
70 new String[]{"ORD-003", "SHIPPED", "0"},
71 new String[]{"ORD-004", "PENDING", "30"}
72 );
73
74 for (String[] req : requests) {
75 try {
76 cancelOrder(req[0], req[1], Integer.parseInt(req[2]));
77 } catch (OrderException oe) {
78 // Catches ALL OrderException subtypes with ONE block —
79 // useful for generic logging/metrics that apply to any order failure
80 System.out.println(" [LOG] Order failure for " + oe.getOrderId() +
81 " [" + oe.getClass().getSimpleName() + "]: " + oe.getMessage());
82 }
83 }
84
85 System.out.println();
86
87 System.out.println("=== Catching SPECIFIC types for different recovery actions ===");
88 try {
89 cancelOrder("ORD-005", "SHIPPED", 0);
90 } catch (OrderAlreadyShippedException oase) {
91 // SPECIFIC catch — access to shippedDate, which the base type does not expose
92 System.out.println(" Shipped on " + oase.getShippedDate() +
93 " — offer return instead of cancellation");
94 } catch (OrderException oe) {
95 // FALLBACK — any other order exception
96 System.out.println(" General order failure: " + oe.getMessage());
97 }
98
99 try {
100 cancelOrder("ORD-006", "PENDING", 48);
101 } catch (OrderCancellationWindowExpiredException ocwee) {
102 // SPECIFIC catch — access to hoursElapsed
103 System.out.println(" " + ocwee.getHoursElapsed() +
104 "h elapsed — offer partial refund instead of cancellation");
105 } catch (OrderException oe) {
106 System.out.println(" General order failure: " + oe.getMessage());
107 }
108 }
109}Output:
=== Catching the FAMILY with one block (generic handling) ===
Cancelled: ORD-001
[LOG] Order failure for ORD-002 [OrderNotFoundException]: Order not found: ORD-002
[LOG] Order failure for ORD-003 [OrderAlreadyShippedException]: Order ORD-003 already shipped on 2026-06-10 — cannot modify
[LOG] Order failure for ORD-004 [OrderCancellationWindowExpiredException]: Order ORD-004 cancellation window expired (30h elapsed, limit is 24h)
=== Catching SPECIFIC types for different recovery actions ===
Shipped on 2026-06-10 — offer return instead of cancellation
48h elapsed — offer partial refund instead of cancellation
Real-World Example — Zomato Restaurant Onboarding Service
A restaurant onboarding service at Zomato validates a partner application against multiple business rules before activating the restaurant on the platform. The service defines a small exception family rooted in RestaurantOnboardingException, with specific subtypes carrying the structured data each failure needs — error codes for the API response, and fields the frontend uses to guide the restaurant owner toward fixing the issue.
1// File: RestaurantOnboardingException.java
2
3// Abstract base — common fields and error code convention for the whole family
4public abstract class RestaurantOnboardingException extends RuntimeException {
5
6 private final String restaurantId;
7 private final String errorCode;
8
9 protected RestaurantOnboardingException(
10 String restaurantId, String errorCode, String message) {
11 super(message);
12 this.restaurantId = restaurantId;
13 this.errorCode = errorCode;
14 }
15
16 public String getRestaurantId() { return restaurantId; }
17 public String getErrorCode() { return errorCode; }
18}1// File: MissingDocumentException.java
2
3import java.util.List;
4
5public class MissingDocumentException extends RestaurantOnboardingException {
6
7 private final List<String> missingDocuments;
8
9 public MissingDocumentException(String restaurantId, List<String> missingDocuments) {
10 super(restaurantId, "MISSING_DOCUMENTS",
11 "Restaurant " + restaurantId + " is missing required documents: " +
12 String.join(", ", missingDocuments));
13 this.missingDocuments = List.copyOf(missingDocuments);
14 }
15
16 public List<String> getMissingDocuments() { return missingDocuments; }
17}1// File: InvalidFssaiLicenseException.java
2
3public class InvalidFssaiLicenseException extends RestaurantOnboardingException {
4
5 private final String licenseNumber;
6 private final String reason;
7
8 public InvalidFssaiLicenseException(
9 String restaurantId, String licenseNumber, String reason) {
10 super(restaurantId, "INVALID_FSSAI_LICENSE",
11 "FSSAI license " + licenseNumber + " for restaurant " + restaurantId +
12 " is invalid: " + reason);
13 this.licenseNumber = licenseNumber;
14 this.reason = reason;
15 }
16
17 public String getLicenseNumber() { return licenseNumber; }
18 public String getReason() { return reason; }
19}1// File: ServiceAreaNotCoveredException.java
2
3public class ServiceAreaNotCoveredException extends RestaurantOnboardingException {
4
5 private final String pincode;
6
7 public ServiceAreaNotCoveredException(String restaurantId, String pincode) {
8 super(restaurantId, "SERVICE_AREA_NOT_COVERED",
9 "Pincode " + pincode + " for restaurant " + restaurantId +
10 " is outside the current delivery network");
11 this.pincode = pincode;
12 }
13
14 public String getPincode() { return pincode; }
15}1// File: RestaurantOnboardingService.java
2
3import java.util.List;
4import java.util.Map;
5import java.util.Set;
6
7public class RestaurantOnboardingService {
8
9 private static final Set<String> COVERED_PINCODES =
10 Set.of("560001", "560034", "560102", "400001");
11
12 private static final List<String> REQUIRED_DOCUMENTS =
13 List.of("FSSAI_LICENSE", "GST_CERTIFICATE", "BANK_DETAILS", "MENU_CARD");
14
15 public void onboard(
16 String restaurantId,
17 List<String> submittedDocuments,
18 String fssaiLicenseNumber,
19 boolean fssaiVerified,
20 String pincode) {
21
22 // Check 1 — all required documents present
23 List<String> missing = REQUIRED_DOCUMENTS.stream()
24 .filter(doc -> !submittedDocuments.contains(doc))
25 .toList();
26 if (!missing.isEmpty()) {
27 throw new MissingDocumentException(restaurantId, missing);
28 }
29
30 // Check 2 — FSSAI license verification
31 if (!fssaiVerified) {
32 throw new InvalidFssaiLicenseException(
33 restaurantId, fssaiLicenseNumber, "verification failed with FSSAI database");
34 }
35
36 // Check 3 — service area coverage
37 if (!COVERED_PINCODES.contains(pincode)) {
38 throw new ServiceAreaNotCoveredException(restaurantId, pincode);
39 }
40
41 System.out.println(" Restaurant " + restaurantId + " ACTIVATED on platform");
42 }
43
44 // API-style handler — converts exception family into a response map
45 public Map<String, Object> onboardWithResponse(
46 String restaurantId, List<String> submittedDocuments,
47 String fssaiLicenseNumber, boolean fssaiVerified, String pincode) {
48
49 try {
50 onboard(restaurantId, submittedDocuments, fssaiLicenseNumber, fssaiVerified, pincode);
51 return Map.of("status", "ACTIVATED", "restaurantId", restaurantId);
52
53 } catch (MissingDocumentException mde) {
54 return Map.of(
55 "status", "REJECTED",
56 "errorCode", mde.getErrorCode(),
57 "missingDocuments", mde.getMissingDocuments());
58
59 } catch (InvalidFssaiLicenseException ifle) {
60 return Map.of(
61 "status", "REJECTED",
62 "errorCode", ifle.getErrorCode(),
63 "licenseNumber", ifle.getLicenseNumber());
64
65 } catch (ServiceAreaNotCoveredException sance) {
66 return Map.of(
67 "status", "REJECTED",
68 "errorCode", sance.getErrorCode(),
69 "pincode", sance.getPincode());
70
71 } catch (RestaurantOnboardingException roe) {
72 // Catches any FUTURE subtype added to the family without code changes here
73 return Map.of("status", "REJECTED", "errorCode", roe.getErrorCode());
74 }
75 }
76
77 public static void main(String[] args) {
78 RestaurantOnboardingService service = new RestaurantOnboardingService();
79
80 System.out.println("=== Successful onboarding ===");
81 System.out.println(service.onboardWithResponse(
82 "REST-1001",
83 List.of("FSSAI_LICENSE", "GST_CERTIFICATE", "BANK_DETAILS", "MENU_CARD"),
84 "FSSAI-12345", true, "560001"));
85
86 System.out.println();
87
88 System.out.println("=== Missing documents ===");
89 System.out.println(service.onboardWithResponse(
90 "REST-1002",
91 List.of("FSSAI_LICENSE", "GST_CERTIFICATE"),
92 "FSSAI-67890", true, "560034"));
93
94 System.out.println();
95
96 System.out.println("=== Invalid FSSAI license ===");
97 System.out.println(service.onboardWithResponse(
98 "REST-1003",
99 List.of("FSSAI_LICENSE", "GST_CERTIFICATE", "BANK_DETAILS", "MENU_CARD"),
100 "FSSAI-00000", false, "400001"));
101
102 System.out.println();
103
104 System.out.println("=== Service area not covered ===");
105 System.out.println(service.onboardWithResponse(
106 "REST-1004",
107 List.of("FSSAI_LICENSE", "GST_CERTIFICATE", "BANK_DETAILS", "MENU_CARD"),
108 "FSSAI-11111", true, "999999"));
109 }
110}Output:
=== Successful onboarding ===
{status=ACTIVATED, restaurantId=REST-1001}
=== Missing documents ===
{status=REJECTED, errorCode=MISSING_DOCUMENTS, missingDocuments=[BANK_DETAILS, MENU_CARD]}
=== Invalid FSSAI license ===
{status=REJECTED, errorCode=INVALID_FSSAI_LICENSE, licenseNumber=FSSAI-00000}
=== Service area not covered ===
{status=REJECTED, errorCode=SERVICE_AREA_NOT_COVERED, pincode=999999}
Best Practices
Make custom exceptions immutable — set fields once in the constructor, never add setters. An exception object represents a fact about something that already happened. Adding setters invites code that catches an exception, mutates it, and rethrows it — which makes the exception's history confusing and can corrupt the stack trace's meaning. All fields should be final and assigned only in the constructor.
Name custom exceptions for the failure, not the layer. InsufficientStockException describes what went wrong. ServiceLayerException describes where the code lives — it tells callers nothing about the actual problem. The name should let a developer reading a catch block understand the failure without opening the exception's source file.
Group related exceptions under a common abstract base only when callers genuinely need to catch the group. If every catch site in your codebase ends up writing catch (OrderException e) and then immediately checking instanceof to figure out which specific subtype it is, the hierarchy is not earning its complexity — separate, unrelated exceptions with no common base would serve the same purpose with less ceremony. Build a hierarchy when there is a real, recurring need to handle the family generically (logging, metrics, a catch-all API error mapper) AND specifically (different recovery per subtype).
Always provide the message-and-cause constructor, even if you think you will never wrap another exception. Code evolves. A custom exception that starts as "this is always a fresh failure, never a wrapped one" frequently ends up needing to wrap an underlying IOException or SQLException six months later. Having super(message, cause) available from day one means that change does not require modifying the exception class itself — only the throw site.
Common Mistakes
Mistake 1 — Forgetting to Call a super Constructor With the Message
1// WRONG — message and cause are never passed to Throwable
2// getMessage() returns null, getCause() returns null — debugging is much harder
3public class BadOrderException extends RuntimeException {
4 private final String orderId;
5
6 public BadOrderException(String orderId) {
7 // super() implicitly called — message is NEVER SET
8 this.orderId = orderId;
9 }
10
11 public String getOrderId() { return orderId; }
12}
13// e.getMessage() returns null — printStackTrace() shows no message at all
14
15// CORRECT — always pass a message to super()
16public class GoodOrderException extends RuntimeException {
17 private final String orderId;
18
19 public GoodOrderException(String orderId) {
20 super("Order processing failed: " + orderId); // message IS set
21 this.orderId = orderId;
22 }
23
24 public String getOrderId() { return orderId; }
25}Mistake 2 — Creating a New Exception Class for Every Slightly Different Message
1// WRONG — five nearly-identical classes, each with one hardcoded message variant
2class InvalidEmailException extends RuntimeException {
3 InvalidEmailException() { super("Invalid email format"); }
4}
5class InvalidPhoneException extends RuntimeException {
6 InvalidPhoneException() { super("Invalid phone format"); }
7}
8class InvalidPincodeException extends RuntimeException {
9 InvalidPincodeException() { super("Invalid pincode format"); }
10}
11// ...two more like this — five classes for what is really ONE concept
12
13// CORRECT — one exception class, parameterized by WHAT FAILED VALIDATION
14class InvalidFieldException extends RuntimeException {
15 private final String fieldName;
16 private final String invalidValue;
17
18 InvalidFieldException(String fieldName, String invalidValue) {
19 super("Invalid value for field '" + fieldName + "': " + invalidValue);
20 this.fieldName = fieldName;
21 this.invalidValue = invalidValue;
22 }
23
24 String getFieldName() { return fieldName; }
25 String getInvalidValue() { return invalidValue; }
26}
27// throw new InvalidFieldException("email", "not-an-email");
28// throw new InvalidFieldException("phone", "12345");Mistake 3 — Putting Mutable State or Heavy Objects Inside an Exception
1// WRONG — exception holds a reference to a large, mutable object
2// The object's state at the time of CATCHING may differ from
3// its state at the time of THROWING — misleading for debugging
4class OrderValidationException extends RuntimeException {
5 private final Order order; // entire mutable Order object — large and mutable
6
7 OrderValidationException(Order order, String reason) {
8 super("Validation failed: " + reason);
9 this.order = order; // if 'order' changes after this, the exception's view changes too
10 }
11
12 Order getOrder() { return order; }
13}
14
15// CORRECT — extract only the immutable, relevant identifying data at throw time
16class OrderValidationExceptionFixed extends RuntimeException {
17 private final String orderId; // just the ID — small, immutable
18 private final double attemptedTotal; // a snapshot value, not a live reference
19
20 OrderValidationExceptionFixed(String orderId, double attemptedTotal, String reason) {
21 super("Validation failed for order " + orderId + ": " + reason);
22 this.orderId = orderId;
23 this.attemptedTotal = attemptedTotal;
24 }
25
26 String getOrderId() { return orderId; }
27 double getAttemptedTotal() { return attemptedTotal; }
28}Mistake 4 — Extending Throwable or Error Directly Instead of Exception
1// WRONG — extending Error suggests a JVM-level, unrecoverable condition
2// Application code should almost NEVER extend Error
3class OutOfInventoryError extends Error { // misleading severity signal
4 OutOfInventoryError(String productId) {
5 super("Out of inventory: " + productId);
6 }
7}
8// Catching code, monitoring tools, and other developers will treat this
9// as if it were as severe as OutOfMemoryError or StackOverflowError —
10// it is not; it is a normal business condition
11
12// CORRECT — extend Exception or RuntimeException for application-level failures
13class OutOfInventoryException extends RuntimeException {
14 OutOfInventoryException(String productId) {
15 super("Out of inventory: " + productId);
16 }
17}
18// Reserve extending Error for genuinely catastrophic, unrecoverable
19// conditions — and in practice, application code almost never does this;
20// Error subclasses are created by the JVM itselfInterview Questions
Q1. How do you create a custom exception in Java?
Extend Exception (for a checked custom exception) or RuntimeException (for unchecked), and provide at least a constructor that accepts a message and calls super(message). A well-designed custom exception typically also provides a constructor accepting (String message, Throwable cause) for wrapping scenarios, and adds domain-specific fields with getters — for example, InsufficientStockException might carry productId, requestedQuantity, and availableQuantity as fields, set once in the constructor and exposed via getters, so catching code can react to the specific numbers without parsing the message string.
Q2. Should a custom exception extend Exception or RuntimeException — what determines this choice?
The decision depends on whether callers have a genuine, differentiated recovery path and whether the failure represents an expected business condition or a programming/system error. Extend Exception (checked) when callers must be forced to acknowledge and handle the failure — InsufficientFundsException where the UI shows a top-up prompt. Extend RuntimeException (unchecked) when the failure is best handled at a single boundary (a global exception handler) or represents a contract violation — InvalidOrderStateException thrown when a method is called on an order in the wrong state. Most modern Java codebases lean toward unchecked custom exceptions to avoid throws-clause pollution through service layers, reserving checked exceptions for cases with genuinely distinct per-caller recovery.
Q3. What constructors should a custom exception class provide?
Following the Throwable convention: a no-argument constructor, a (String message) constructor, a (String message, Throwable cause) constructor, and optionally a (Throwable cause) constructor. In practice, the (String message) and (String message, Throwable cause) forms are used most often — the first for a freshly-detected failure, the second for wrapping an underlying exception while adding domain context. Providing all four, even if only two are currently used, means future code that needs to wrap an exception does not require modifying the custom exception class itself.
Q4. What is the benefit of adding fields to a custom exception instead of putting everything in the message string?
Fields allow catching code to react programmatically without parsing strings. catch (PaymentDeclinedException e) { if (e.getReason() == DeclineReason.BANK_UNAVAILABLE) retry(); } is reliable and type-safe. catch (RuntimeException e) { if (e.getMessage().contains("BANK_UNAVAILABLE")) retry(); } is fragile — it breaks if the message wording changes, and string-matching on exception messages is a maintenance hazard that code reviews routinely flag. Fields also enable structured logging and API error responses that include machine-readable error codes alongside human-readable messages.
Q5. When should you build an exception hierarchy with a common abstract base class?
When there is a genuine recurring need to catch a family of related exceptions generically — for logging, metrics, or a centralized API error mapper — while ALSO sometimes needing to catch specific subtypes for differentiated recovery. An abstract OrderException base carrying a common orderId field, extended by OrderNotFoundException, OrderAlreadyShippedException, and others, lets a generic handler do catch (OrderException e) { logFailure(e.getOrderId(), e); } while a specific handler elsewhere does catch (OrderAlreadyShippedException e) { offerReturn(e.getShippedDate()); }. If no code ever needs the generic catch, the hierarchy adds complexity without benefit — flat, unrelated exception classes are simpler.
Q6. What happens if a custom exception class does not call super() with a message?
If the constructor does not explicitly call super(message) (or any super(...) form that sets the message), Java implicitly calls the no-argument super(), and getMessage() returns null for that exception instance. This means printStackTrace() shows the exception type with no message, and any logging code that does logger.error(exception.getMessage()) logs null. This is a common mistake in fresher pull requests — the custom exception class compiles fine and even "works" in the sense that it is thrown and caught correctly, but it carries no diagnostic information, which only becomes apparent when someone tries to debug a production issue using it.
FAQs
Can a custom exception have multiple constructors?
Yes — and it should, for the same reason any well-designed class has multiple constructors: different call sites have different information available. A constructor taking just a product ID for a "not found" scenario, and another taking a product ID plus a cause for a "lookup failed due to infrastructure error" scenario, are both valid and commonly coexist on the same exception class.
Should custom exceptions be in their own package?
Many teams organize custom exceptions into a dedicated package (com.example.orders.exceptions) or co-locate them with the domain classes they relate to (com.example.orders alongside Order.java). Either convention works; the important thing is consistency across the codebase so developers know where to look. Co-location is often preferred for smaller exception families — package separation tends to help in larger codebases with many domain modules, each having their own exception sets.
Can a custom exception class be generic (use type parameters)?
Technically yes, but it is rare and usually unnecessary. Throwable and its subclasses are not designed with generics in mind, and a generic exception class (class ValidationException<T> extends RuntimeException) introduces complexity — catch clauses cannot use generic types (catch (ValidationException<String> e) is not valid Java) due to type erasure. If you need to carry a typed value, use a non-generic exception class with a field of type Object or a sealed set of specific subclasses instead.
Is it bad practice to have too many custom exception classes?
It depends on whether each class represents a genuinely distinct failure mode that callers handle differently. Five custom exceptions, each handled differently by callers, are five legitimate classes. Five custom exceptions that differ only in their hardcoded message text, all caught and handled identically, should likely be one parameterized exception class (see Common Mistake 2). The test: if removing a specific exception class and replacing all its throw sites with a more general one would not change any catching code's behaviour, the specific class was probably unnecessary.
Do custom exceptions need to be serializable?
Exception and RuntimeException already implement Serializable (inherited from Throwable), so custom exceptions are serializable by default as long as all their fields are also serializable types (or marked transient). This matters primarily in distributed systems — RMI, certain messaging frameworks, or any scenario where an exception object crosses a serialization boundary. If a custom exception field holds a non-serializable type (a database connection, a thread), mark it transient or avoid storing it directly — store an identifying string instead.
Can you catch a custom exception using its parent class type?
Yes — this is standard polymorphic catching and is one of the main reasons to build an exception hierarchy. If InsufficientStockException extends RuntimeException, then catch (RuntimeException e) catches it (along with every other unchecked exception). If you built a custom hierarchy with InsufficientStockException extends InventoryException, then catch (InventoryException e) catches it along with any other InventoryException subtype, but not unrelated RuntimeException subtypes outside that hierarchy.
Summary
A custom exception class is a small, focused addition to Java's exception model: extend Exception or RuntimeException, call the appropriate super() constructor to set the message and optionally the cause, and add whatever domain fields make the failure actionable for catching code. The choice between checked and unchecked should reflect whether callers have a genuinely different recovery path per exception type — not be a default.
The decisions that separate well-designed custom exceptions from clutter: immutable fields set once in the constructor, names that describe the failure rather than the architectural layer, structured fields and error codes instead of string-parseable messages, and a hierarchy built only when generic-and-specific catching are both genuinely needed.
Every custom exception in a production codebase should answer one question clearly: when this is caught, what does the catching code now know that it did not know before, and what can it do differently because of it? If the answer is "nothing beyond what a generic RuntimeException with this message would provide," the custom exception is not pulling its weight — and that is a useful filter to apply during code reviews of new exception classes.
What to Read Next
| Topic | Link |
|---|---|
| The full difference between checked and unchecked exceptions and how it shapes custom exception design | Java Checked vs Unchecked Exceptions → |
| How throw raises exceptions and how throws declarations propagate custom checked types | Java throw Keyword → |
| How try, catch, and multi-catch handle custom exception hierarchies | Java try-catch → |
| What finally guarantees when custom exceptions are thrown mid-cleanup | Java finally → |
| The complete exception handling foundation — all five mechanisms together | Java Exception Handling → |