rethrow statement propagates a currently caught exception out of the enclosing catch clause, preserving the original exception object and its associated stack trace. It functions exclusively within exception handling blocks to transfer control to the nearest enclosing exception handler in the call stack.
Syntax
Therethrow keyword appears as a standalone statement within an on or catch block.
Operational Semantics
- Scope Validity: The statement is syntactically valid only within the scope of an exception handler (
catchoronblock). - Control Flow: Upon execution,
rethrowimmediately terminates the current function’s execution and transfers control to the next exception handler up the call stack. - Exception Integrity: Unlike explicitly throwing the caught exception object (e.g.,
throw e),rethrowmaintains the metadata and history of the original exception event.
Stack Trace Preservation
The technical distinction betweenthrow and rethrow centers on StackTrace management.
throw e: Initiates a new exception event. The stack trace is reset to the line wherethrow eis executed, obscuring the original source of the error.rethrow: Resumes the propagation of the original exception. The stack trace retains the complete history starting from the initial point of failure.
Visualization of Stack Trace Behavior
Master Dart with Deep Grasping Methodology!Learn More





