Java / Java 21 Coding Standards Interview Questions
Describe the standard naming convention for custom exception classes in Java?
A custom exception class is named as a noun phrase ending in Exception, describing the specific failure it represents, such as InsufficientFundsException or OrderNotFoundException, rather than a vague name like AppException or ErrorType1.
public class OrderNotFoundException extends RuntimeException { public OrderNotFoundException(String orderId) { super("Order not found: " + orderId); } }
Standards also require that custom exceptions provide constructors matching the standard four-argument pattern inherited from Throwable - message only, cause only, message and cause, and no-arg - so the exception composes cleanly with exception chaining and logging frameworks that expect those constructors to exist.
The choice between extending RuntimeException or a checked Exception should reflect whether callers can reasonably be expected to recover from the failure; if not, an unchecked exception avoids forcing every caller up the stack to handle or declare it.
More Related questions...