A custom exception is an application-defined exception type named for a specific failure. At a business layer, API boundary, or user-facing interface, it can translate a technical failure into a meaningful domain error. section 7.2.4
Choose an intention-revealing name, even if the class is otherwise small. Keep captured data immutable, and preserve the original cause so technical details remain available for diagnosis rather than exposed to users or clients. section 7.2.4
public class DataProcessingException extends Exception {
private final String data;
public DataProcessingException(String data,
String message, Exception exception) {
super(message, exception);
this.data = data;
}
}
This Java exception captures the input and passes the original exception to its superclass. section 7.2.4
int dataToTransform = 0;
try {
dataToTransform = Integer.parseInt(data);
} catch (NumberFormatException exception) {
throw new DataProcessingException(data, "Processing error", exception);
}
A parsing failure becomes a named processing failure without losing its cause. section 7.2.4
In Python, use exception chaining with `raise ... from ...` to preserve the original error. Let a higher layer decide how to handle a failure when a low-level action lacks the broader context. section 7.2.4


