-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#21-Exeception_Handelling.dart
More file actions
65 lines (54 loc) · 2.38 KB
/
#21-Exeception_Handelling.dart
File metadata and controls
65 lines (54 loc) · 2.38 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void main(){
// <!-- this exception exception cannot work in some ide so please suggest some exception and fork it ---!>
//Exception Handelling in Dart.
//it is use when the code can be crash due to code bug we use exception handeling.
/* Four type of exception Handelling
* try keyword using on --> when we know the exception .
* try keyord using catch --> when we don't know the exception.
* try keyword using catch and finally --> it is like catch keyword but with an extra argument.
* try keyword using catch and stackTrace object --> when we don't know the exception and want to know the step before the exception.
*/
//1 .Exception handelling using 'try' and 'on' keyword.
print("case:1");
try{
var result = 12 / 0; //if we devided it by 0 it will be crash, so we have to use exception handelling
print("the result is $result");
}on IntegerDivisionByZeroException{//on clause is used when we know the exception name, so we can feedback the user depending on the exception.
print("it Canot divided by zero");
}
print("");
//2. Exception Handelling using 'try' and 'catch' Keyword.
print("case:2");
//When we don't know the exception name we can use 'Catch' keyword
try {
int result = 50 ~/ 0; //
print("the result is $result");
} catch(e) { // 'e' Argument refers to the exception object.
print("the Exception is $e");
}
print("");
//3. Exception Handelling using 'try' and 'catch' and 'finally ' Keyword.
print("case:3");
//When we don't know the exception name we can use 'Catch' keyword and finally keyword for an extra information .
//finally statement will execute when there is no exception.
try {
int result = 50 ~/ 0; //
print("the result is $result");
} catch(e) { // 'e' Argument refers to the exception object
print("the Exception is $e");
}finally{
print("it is a exception message");
}
print("");
//4. Exception Handelling using 'try' and 'catch' Keyword with stacktrace.
print("case:4");
//When we don't know the exception name we can use 'Catch' keyword.
try {
int result = 50 ~/ 0; //
print("the result is $result");
} catch(e,s) { // 'e' Argument refers to the exception object 's' is used for stacktrace object.
print("the Exception is $e");
print(s);//will show to step before excption or stacktrace.
}
print("");
}