liveBook · Manning

Exception

As explained in Rust in Action: Systems programming concepts and techniques

An exception is a synchronous or otherwise special execution event that interrupts normal program flow because of an error or exceptional condition.

A CPU exception interrupts normal execution when the CPU encounters a problem while processing an instruction, such as an illegal instruction or an invalid memory address. It is also called a synchronous interrupt. Unlike a hardware interrupt from a device, it arises during the CPU’s own processing. section 12.1.1

Exception names describe whether execution can recover:

  • A fault is recoverable. A page fault, for example, gives the operating system a chance to address a memory-access problem. section 12.1
  • A trap is recoverable; CPU-detected integer overflow is one example. section 12.1
  • An abort is unrecoverable and terminates the application. section 12.1

When an interrupt occurs, the CPU transfers control to handler code whose location was established during boot. An operating-system signal is different: it interrupts an application’s control flow, and many signals can be ignored. Treating signals as interrupts can be a useful learning model, but it does not make them the same thing. section 12.1.1 section 12.11

A minimal kernel handler

A small Rust kernel can declare an empty `eh_personality` function. During exception handling, a personality function is called as stack frames are unwound to determine whether a frame can handle the exception. An empty function handles none: the kernel still crashes when an error occurs. It is a starting framework, not recovery. section 11.4.1 section 11.4

Do not confuse this CPU meaning with every use of “error” in Rust. A fallible conversion can return `Result`, containing either a value or an error; calling `unwrap()` on a failure can crash the program. That is a programming-language error-handling example, not a definition of a CPU exception. section 2.3.3 section 12.1

Figure 12.1 A visual taxonomy of how the terms interrupt, exception, trap, and fault interact within Intel’s x86 family of CPUs. Note that signals do not appear within this figure. Signals are not interrupts.

Use the taxonomy figure to locate faults, traps, and aborts under exceptions and to keep signals outside the interrupt family. section 12.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines fault, trap, and abort, then implements only a minimal personality routine rather than recovery.
Examples
Uses page faults, integer overflow, and an empty Rust `eh_personality` function as concrete cases.
What it assumes
Introduces stack unwinding and CPU-interruption terminology rather than assuming prior knowledge of either.
What you can do after
Evaluate/understand it by classifying CPU exceptions and recognizing why a minimal kernel personality function does not recover from them.
How it connects
Connects exceptions to synchronous interrupts, stack unwinding, operating-system handlers, signals, and compiler intrinsics.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.