Error Handling
Try, Catch, Finally
catch handles thrown exceptions, while finally runs whether the protected work succeeds or fails.
Program
Play the program to see cleanup happen on both the normal path and the failure path.
try_catch_finally.dart
Replay: real traced execution (multi-file project)
void main() {
var shouldFail = false;
var log = <String>[];
try {
log.add('open');
if (shouldFail) throw StateError('boom');
log.add('work');
} catch (e) {
log.add('catch');
} finally {
log.add('close');
}
print(log.join('>'));
}
void main() {
var shouldFail = true;
var log = <String>[];
try {
log.add('open');
if (shouldFail) throw StateError('boom');
log.add('work');
} catch (e) {
log.add('catch');
} finally {
log.add('close');
}
print(log.join('>'));
}
shouldFail ← false
1void main() {2 var shouldFail = false;3 var log = <String>[];values this stepfalseshouldFaillog ← []
2var shouldFail = false;3var log = <String>[];4try {values this step[]loglog ← [open]
4try {5 log.add('open');6 if (shouldFail) throw StateError('boom');values this step[] → [open]logthrow ← false
5log.add('open');6if (shouldFail) throw StateError('boom');7log.add('work');values this stepfalsethrowfalseshouldFaillog ← [open, work]
6 if (shouldFail) throw StateError('boom');7 log.add('work');8} catch (e) {values this step[open] → [open, work]loglog ← [open, work, close]
10} finally {11 log.add('close');12}values this step[open, work] → [open, work, close]logprint(log.join('>'));
12 }13 print(log.join('>'));14}outputopen>work>closevalues this step[open, work, close]log
shouldFail ← true
1void main() {2 var shouldFail = true;3 var log = <String>[];values this steptrueshouldFaillog ← []
2var shouldFail = true;3var log = <String>[];4try {values this step[]loglog ← [open]
4try {5 log.add('open');6 if (shouldFail) throw StateError('boom');values this step[] → [open]logthrows ← StateError('boom')
5log.add('open');6if (shouldFail) throw StateError('boom');7log.add('work');values this stepStateError('boom')throwstrueshouldFaillog ← [open, catch]
8} catch (e) {9 log.add('catch');10} finally {values this step[open] → [open, catch]loglog ← [open, catch, close]
10} finally {11 log.add('close');12}values this step[open, catch] → [open, catch, close]logprint(log.join('>'));
12 }13 print(log.join('>'));14}outputopen>catch>closevalues this step[open, catch, close]log
try
The `try` block contains work that might throw.
catch
`catch (e)` runs only for the thrown path.
finally
`finally` always runs, making it suitable for cleanup.