ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
catch clause in Dart is an exception-handling construct used immediately following a try block to intercept and process objects thrown during execution. It prevents unhandled exceptions from terminating the current isolate and provides access to the thrown object and its execution context.
In Dart, because any non-null object can be thrown, the catch clause is designed to handle arbitrary types, though it is most commonly used with instances of Exception or Error.
Syntax and Parameters
Thecatch clause accepts one or two parameters:
- Exception Object (
e): The first parameter captures the thrown object. By default, its type isObject. - Stack Trace (
s): The optional second parameter captures an instance ofStackTrace, representing the call stack at the exact point the exception was thrown.
Type-Specific Interception (on keyword)
To handle different exceptions polymorphically, Dart pairs the catch clause with the on keyword. The on keyword filters exceptions based on their runtime type.
- If you need to inspect the exception object, use
on Type catch (e). - If you only need to handle the type but do not need the object itself, omit the
catchclause entirely and useon Type.
Evaluation Mechanics
- Sequential Evaluation: Multiple
on/catchblocks are evaluated sequentially from top to bottom. The Dart runtime executes the first block where the thrown object’s runtime type is a subtype of the type specified in theonclause. - Catch-All Behavior: A standalone
catchblock (without anonmodifier) acts as a catch-all. It is equivalent toon Object catch (e). It must always be placed at the bottom of the handler chain to prevent unreachable code errors. - Propagation via
rethrow: Within acatchblock, therethrowkeyword can be used to propagate the intercepted exception up the call stack. Unlike throwing a new exception or explicitly throwing the captured object (throw e;),rethrowpreserves the originalStackTraceof the exception.
Master Dart with Deep Grasping Methodology!Learn More





