A failure path is a sequence of execution possibilities through which an operation reaches an unsuccessful or exceptional outcome. Good design identifies these paths before users encounter infinite spinners, raw stack traces, or abandoned workflows. Graceful handling may use domain-specific exceptions or explicit result types, while unexpected programming errors should fail fast. section 7.1.1 section 7.2.2
Make failure paths readable and operationally useful:
- Catch specific expected failures instead of routing every exception through generic fallback logic. section 7.2.2
- Keep protected blocks tight enough that readers can see which related operations may fail and what follows. section 7.2.2
- Separate minor failures from serious signals in logs so monitoring can reveal threats to robustness or security. section 7.2.2
- Prevent raw stack traces and sensitive implementation details from reaching users. section 7.1.1
Reading a file
A broad catch obscures whether opening, reading, or processing failed. Specific catches make the failure narrative visible: report a missing file with a warning, report read failure as an error, and keep the protected operation compact. section 7.2.2
try (BufferedReader reader = new BufferedReader(
new FileReader("data.txt"))) {
String line = reader.readLine();
processLine(line);
} catch (FileNotFoundException e) {
logger.warn("DATA_01: File 'data.txt' not found.", e);
} catch (IOException e) {
logger.error("DATA_02: Failed to read from 'data.txt'.", e);
}
Compare generic exception handling with specific FileNotFoundException and IOException paths, including distinct diagnostic messages. section 7.2.2
