Affiliate disclosure
Book titles on this page link to Amazon. As an Amazon Associate, DataField.Dev earns from qualifying purchases — at no additional cost to you.
Further Reading: Error Handling, Debugging, and Defensive Programming
A guide to going deeper on the three strands of this chapter. Sources are tagged Tier 1 (works we are confident exist and recommend) and Tier 2 (real and worth seeking, but confirm the current edition/URL yourself). Much of the most useful material here is tool documentation — the compiler and debugger manuals — which repays a careful read far more than its dryness suggests.
The language reference (what the standard actually says)
- Metcalf, Reid, and Cohen, Modern Fortran Explained (Oxford University Press). The authoritative
account of error termination,
error stop, and thestat/errmsgspecifiers, with the precise standard semantics this chapter summarizes. Its chapters on error handling and on I/O are the reference to pin down a rule. Tier 1. - The Fortran 2018 standard (ISO/IEC 1539-1:2018). The primary source for the meaning of
error stop, normal vs error termination, and theiso_fortran_envnamed constants (error_unit,iostat_end,stat_failed_image). Consult it when two secondary sources disagree. Tier 1.
The tools (read the manuals — they are the skill)
- The GCC /
gfortranmanual, "Code Gen Options" and "Debugging Options." The definitive description of-fcheck,-ffpe-trap,-fbacktrace,-finit-real, and the-fsanitizefamily. Every flag in §13.3 and Appendix C is documented here with its exact behavior and caveats. Tier 1. - The GDB manual (
sourceware.org/gdb), including its Fortran-language notes. How to set breakpoints, inspect variables and array slices, and read a backtrace — with the Fortran-specific details (1-based indexing, array-section printing, module variables) that §13.4 only sketched. Tier 1. - The Valgrind documentation (
valgrind.org), the Memcheck manual. How to read leak reports and invalid-access diagnostics, and how to interpret them for compiled Fortran. Pair it with the AddressSanitizer documentation (part of the Clang/GCC docs) for the faster, compile-in alternative. Tier 1 (Valgrind); Tier 2 (ASan specifics).
Defensive programming and debugging as a craft
- Brian W. Kernighan and P. J. Plauger, The Elements of Programming Style. The source of this chapter's epigraph, and still the most quotable short book on writing code that is simple enough to debug. Its counsel — write clearly, check your inputs, do not be too clever — is language-agnostic and timeless. Tier 1.
- Brian W. Kernighan and Rob Pike, The Practice of Programming (Addison-Wesley). Chapter 5, "Debugging," and Chapter 6, "Testing," are a masterclass in the method Case Study 1 applied: reproduce, make it loud, reason from the evidence. Tier 1.
- Steve McConnell, Code Complete (Microsoft Press), the chapter on defensive programming. A thorough, practical treatment of assertions, preconditions/postconditions, and the "how much checking is enough" question, from a general-software perspective that maps cleanly onto §13.5. Tier 1.
The Fortran community
- The Fortran stdlib (
stdlib.fortran-lang.org). The community standard library includes error-handling utilities — anerror_stopwrapper, acheckroutine, anoptvalfor optional arguments, and a logger — that show idiomatic modern patterns you can adopt or imitate. Tier 1. - The Fortran Discourse (
fortran-lang.discourse.group). Search it for real threads on-ffpe-trapsurprises, gdb-with-Fortran tips, and error-handling design; the community debates these openly and helpfully. Tier 1.
Suggested order
- Skim the gfortran "Debugging Options" page and actually try
-fcheck=alland-ffpe-trapon a small broken program of your own — ten minutes here teaches more than any prose. - Read the debugging chapter of Kernighan and Pike for the method; it is short and changes how you approach a failure.
- Keep Metcalf/Reid/Cohen on the shelf for the exact semantics of
error stopandstatwhen you need to be sure. - When you first meet a memory bug in pointer or C-interop code, read the Valgrind Memcheck manual then — it means far more with a real leak in front of you.