liveBook · Manning

User-defined exception

As explained in The Art of Code

A custom exception is an application-defined exception type whose name and structure represent a specific failure condition.

A user-defined exception gives a failure an intention-revealing name. Translate low-level failures into a domain exception or controlled response at a business, API, or user-facing boundary; keep technical detail in logs. section 7.2.4

Make the exception carry the information known when it is created, and keep that state immutable. An apparently small class can make the code’s failure story clearer than a generic exception. section 7.2.4

Wrap parsing without losing the cause

When parsing input fails, throw DataProcessingException with the original data, a meaningful message, and the caught NumberFormatException as its cause. The caller sees a domain-level failure while diagnostics retain the root cause. section 7.2.4

int dataToTransform = 0;
try {
  dataToTransform = Integer.parseInt(data);
} catch (NumberFormatException exception) {            
  throw new DataProcessingException(data, "Processing error", exception);
}

This listing turns a NumberFormatException into a named data-processing failure while preserving the original exception. section 7.2.4

Do not replace an original error without preserving it. Otherwise logs can report only that processing failed, obscuring whether the cause was a missing file, permissions, malformed input, or another technical problem. section 7.2.4

How The Art of Code covers it

How deep it goes
Explains naming, immutable context, wrapping, and cause preservation, without defining exception runtime internals.
Examples
Uses data parsing to wrap NumberFormatException as DataProcessingException, with a Python exception-chaining counterpart.
What it assumes
Assumes familiarity with try/catch-style error handling while showing Java and Python wrapping forms.
What you can do after
Apply it to name domain failures and retain original causes for useful diagnostics.
How it connects
Connects custom exceptions with exception chaining, logging, API boundaries, and domain errors.

Also explained in

Compare how 3 books explain user-defined exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.