-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception_handling.dart
More file actions
49 lines (44 loc) · 1.08 KB
/
exception_handling.dart
File metadata and controls
49 lines (44 loc) · 1.08 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
void main() {
print("CASE 1:");
try {
int result = 12 ~/ 0;
print("Result is: $result");
} on IntegerDivisionByZeroException {
print("Can't divide by zero"); //e is for exception
}
print("\nCASE 2:");
try {
int result = 12 ~/ 0;
print("Result is: $result");
} catch (e, s) {
print("The exception thrown is: $e"); //e is for exception
print("StackTrace: $s"); //s is for stacktrace
}
print("\nCASE 3:");
try {
int result = 12 ~/ 3;
print("Result is: $result");
} catch (e) {
print("The exception thrown is: $e"); //e is for exception
} finally {
//finally clause is always executed no matter if there is exception or not
print("This is finally clause and is always executed");
}
print("\nCASE 5");
try {
deposit(-500);
} catch (e) {
print(e.errorMessage());
}
}
/**********CUSTOM EXCEPTION**********/
class DepositException implements Exception {
String errorMessage() {
return "You can't enter amount less than 0";
}
}
void deposit(int amount) {
if (amount < 0) {
throw new DepositException();
}
}