Skip to content

Instantly share code, notes, and snippets.

@madmag77
Last active March 2, 2021 06:16
Show Gist options
  • Save madmag77/e67aa709e1792e134756db3c2e1683e6 to your computer and use it in GitHub Desktop.
Save madmag77/e67aa709e1792e134756db3c2e1683e6 to your computer and use it in GitHub Desktop.
[Dart article] Handling exceptions in synchronous code
try {
final _ = 100 ~/ 0;
} on IntegerDivisionByZeroException {
print('Cant divide to zero');
} finally {
print('Clean-up done 1');
}
print('\n\n');
try {
throw FormatException('format is wrong');
} on IntegerDivisionByZeroException {
print('Cant divide to zero');
} on FormatException catch (e) {
print('Format exceptions $e');
} finally {
print('Clean-up done 2');
}
print('\n\n');
try {
print('Allocate something 3');
throw 'Other error occurred';
} on IntegerDivisionByZeroException {
print('Cant divide to zero');
} on FormatException catch (e) {
print('Format exceptions: $e');
} catch (e, s) {
print('Unknown exceptions: $e, with stack: \n$s');
rethrow;
} finally {
print('Clean-up something 3');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment