-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo07_error_handling.js
253 lines (216 loc) · 5.15 KB
/
Demo07_error_handling.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* * Error Handling in ES6
*/
'use strict'
console.clear();
//? Syntax Error
//! console.log("Hello world!!!);
//? Runtime Error || Logic Error
let a = 5;
for(let i = a; i >= 0; i--)
console.log(a / i);
//? Use try...catch...final to handle error
var arr = [1, 2, 3, 4, 5];
try {
for(let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}
} catch (e) {
console.log("Error message: ", e);
} finally {
console.log("=> Complete");
}
//? Call stack property
const trace = () => {
try {
throw new Error("Have an error occured: ");
} catch(e) {
console.log(e.stack);
}
}
const second = () => trace();
const first = () => second(1, 'SEstudio', {});
first("Demo");
//? Case 02 for error handling
console.log("--- Case 02 ---");
let obj = {};
console.log("Init object:", obj);
try {
obj.show();
console.log("Show is completed");
} catch (e) {
console.log(e);
}
console.log("=> Complete");
//? Case 03 for error handling
console.log("--- Case 03 ---");
const getInfo = (fileName) => {
try {
console.log("Code in TRY block: First time");
let fileContent = fileName.read();
console.log("Code in TRY block: Second time");
return fileContent;
} catch(e) {
console.log("Code in CATCH blog. Something Error: ", e);
} finally {
console.log("Code in FINALLY block");
}
return "Cannot read file";
}
//! Test-case 01
console.log("--- Call getInfo(null) ---");
let info = getInfo(null);
console.log("Info = ", info);
//! Test-case 02
console.log("--- Call getInfo(fileObject) ---");
let fileObject = {
name : "file_name",
read : () => { return "File data is loaded"; }
};
let file_data = getInfo(fileObject);
console.log(file_data);
//? Error type in ES6
//! 1. Syntax Error
try {
let x;
eval(x, hello); // function eval() is not defined
} catch(e) {
console.log(e.name);
}
//! 2. Range Error
let num = 1;
try {
num.toPrecision(500); // Out of range
} catch(e) {
console.log(e.name);
}
//! 3. Reference Error
let x;
try {
x = y + 1; // Y is not defined
} catch(e) {
console.log(e.name);
}
//! 4. Type Error
num = 1;
try {
num.toUpperCase(); // num is integer, so cannot call metho toUpperCase()
} catch(e) {
console.log(e.name);
}
//! 5. URI Error
try {
let uri = decodeURI("%%%"); // Cannot URI Decode because "%%%" is not URL
// let uri = decodeURI('https://www.google.com');
console.log(uri);
} catch (e) {
console.log(e.name)
}
//?-------------------- THROW ERROR --------------------
// console.clear();
console.log("--- Test throw any Object ---");
try {
let obj = {};
throw obj;
} catch (e) {
console.log(e);
}
console.log("--- Test throw a Symbol ---");
try {
let symbol = Symbol();
throw symbol;
} catch(e) {
console.log(e);
}
console.log("--- Test throw a Number ---");
try {
let num = 404;
throw num;
} catch(e) {
console.log(e);
}
console.log("--- Test throw a String ---");
try {
let str = "File not found";
throw str;
} catch(e) {
console.log(e);
}
//! Create Object Error
console.log("--- Using Object Error ---");
let myError = new Error("Error something");
console.log("Init object Error");
try {
throw myError;
} catch(e) {
console.log(e);
}
//! Catch complex error
let myErr = new Error('Something error');
let rangeErr = new RangeError();
let evalErr = new EvalError("Eval error");
// random value in [0..9]
let randomValue = Math.floor(Math.random() * 10);
// [0, 1, 2, 3]
let randomObject = randomValue % 4;
console.log("Random object: ", randomObject);
try {
switch(randomObject) {
case 0:
throw myErr;
case 1:
throw rangeErr;
case 2:
throw evalErr;
case 3:
throw "String error is here!!!";
}
} catch(e) {
console.log("Type of error = ", (typeof e));
if (e instanceof RangeError)
console.log("-> RangeError");
else if (e instanceof EvalError)
console.log("-> EvalError");
else if (e instanceof Error)
console.log("-> Error!!!");
else if (typeof e == "string")
console.log("-> String Error");
else
console.log("->> Another Error type");
}
//! Error Property
// console.clear();
let firstErr = new Error();
firstErr.name = "First Error";
firstErr.message = "This is FIRST ERROR";
try {
throw firstErr;
} catch(e) {
console.log("Error Name: ", e.name);
console.log("Error Message: ", e.message);
console.log("Type of Error stack:", (typeof e.stack));
console.log("-----------------");
console.log(e.stack);
}
//! Re-throw Error
console.clear();
const checkScore = (score) => {
if (score < 0 || score > 100) {
throw "Invalid score: " + score;
}
}
const checkPlayer = (name, score) => {
try {
checkScore(score);
} catch(e) {
console.log('Something invalid with player:', name, '>>', e);
throw ("Score " + score + " invalid for player " + name);
}
console.log('OK, Player', name, 'have score', score);
}
try {
checkPlayer("Petter", 90);
checkPlayer("Marry", 110);
} catch(e) {
console.log(e);
}