liveBook · Manning

Exception

As explained in The Art of Code

An exception is a runtime event representing an abnormal condition that interrupts ordinary program execution and can be propagated or handled.

An exception interrupts ordinary execution when an operation cannot complete normally. Decide early how failure will be made explicit, whether recovery is possible, and how resources will be released. Success may be silent; failure should not be. section 2.5 section 7.0

In a functional pipeline, an exception disrupts the expected input-to-output flow. Java also requires a checked IOException from file reading to be handled; wrapping the stream in an outer try-catch does not resolve the compiler complaint inside the pipeline. section 6.3.2

List<ReadResult> contents = filenames.stream()
   .map(filename -> {
     try {
       List<String> content = Files.readAllLines(Path.of(filename));
       return new ReadResult(Optional.of(content),                       #1
         Optional.empty());        #1                                     
     } catch (IOException e) {
       return new ReadResult(Optional.empty(),    #2
         Optional.of(e));                         #2
     }
   })
   .toList();

Catch the file-reading exception at the operation and turn each outcome into a value: content on success or an error on failure. This makes failure available to the caller instead of interrupting the pipeline. section 6.3.2

Alternatively, handle the failure inside the pipeline, for example by returning a default value. If a result wrapper makes the pipeline noisy, extract the file-reading operation behind a clear name; a loop with a local catch can also be a practical choice. section 6.3.2

Choose handling according to the failure and the boundary:

  • Use a domain-specific exception or an explicit result type for an anticipated business failure, so the caller can respond appropriately. section 7.1.1
  • Translate low-level failures before they cross architectural boundaries, but preserve the original cause for diagnosis. section 7.3
  • Keep protected blocks narrow, avoid generic catches without justification, and log useful context without sensitive data. section 7.3
  • Do not expose raw stack traces to users; provide a controlled response that does not reveal sensitive technical details. section 7.1.1

How The Art of Code covers it

How deep it goes
Works through pipeline alternatives and gives practical guidance on catch scope, translation, logging, and cause preservation.
Examples
Uses file-reading streams to contrast an unhandled IOException, a result wrapper, an extracted method, and a loop with local handling.
What you can do after
Evaluate exception-handling choices for pipeline failures and boundary crossings.
How it connects
Connects exceptions to functional pipelines, side effects, result types, stack traces, and architectural boundaries.

Also explained in

Compare how 26 books explain 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.