-
Notifications
You must be signed in to change notification settings - Fork 7
/
58-try-catch.dart
36 lines (31 loc) · 1010 Bytes
/
58-try-catch.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// Catching, or capturing, an exception stops the exception from propagating (unless you rethrow the exception).
/// Catching an exception gives you a chance to handle it:
try {
drinkCoffee();
} on OutOfCoffeeException {
// A specific exception
buyMoreCoffee();
} on Exception catch (error) {
// Anything else that is an exception
print('Unknown exception: $error');
} catch (error) {
// No specified type, handles all
print('Something really unknown: $error');
}
/// Catching the Exception message and StackTrace:
try {
// ···
} on Exception catch (error) {
print('Exception details:\n $error');
} catch (Sring error, StackTrace stackTrace) {
print('Exception details:\n $error');
print('Stack trace:\n $stackTrace');
}
/// Finally Case
try {
drinkCoffee();
} catch (error) {
print('Error: $error'); // Handle the exception first.
} finally { // The will be executed at end independent of exceptions
throwOldCoffee(); // Then clean up.
}