16. Exceptions and Errors¶
Legality Rules
16:1 The Rust programming language lacks exceptions and exception handlers. Instead, the language uses the following tiered error handling scheme:
16:2 A possibly absent value is usually represented using enum
core::option::Option
.16:3 The result of a possibly erroneous computation is usually represented using enum
core::result::Result
.16:4 Erroneous behavior is signaled using macro
core::panic
.
16:5
Enum core::option::Option
indicates whether a value is
either present using core::option::Option::Some
or absent using
core::option::Option::None
.
16:6
Enum core::result::Result
indicates whether a computation completed
successfully and produced a value using core::result::Result::Ok
or
the computation failed with an error using core::result::Result::Err
.
16.1. Panic¶
Legality Rules
16.1:1
A panic is an abnormal program state caused by invoking macro
core::panic
.
Dynamic Semantics
16.1:2
Invoking macro core::panic
has the following runtime effects:
16.1:3 Control flow halts the execution of the current thread.
16.1:4 Control flow of the current thread resumes execution by invoking the function subject to attribute
panic_handler
.
Examples
panic!("This was a terrible mistake!");
16.2. Abort¶
Legality Rules
16.2:1 Abort is the immediate termination of a program.
Dynamic Semantics
16.2:2 Abort has the following runtime effects:
16.2:3 Control flow halts the execution of all threads.
16.2:4 The program terminates.