-
Notifications
You must be signed in to change notification settings - Fork 4
/
ex21_try_catch.js
63 lines (55 loc) · 1.46 KB
/
ex21_try_catch.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
try {
console.log('abc'); // -> abc
conole.log('abc');
} catch (error) {
console.log(error.message); // -> conole is not defined
}
try {
let a = b;
} catch (error) {
console.log("Caught " + error); // -> Caught ReferenceError: b is not defined
}
console.log("We handled the exception!"); // -> we handled the exception!
let a = -2;
try {
a = b;
} catch (error) {
if (error instanceof ReferenceError) {
console.log("Reference error, reset a to -2"); // -> Reference error, reset a to -2
a = -2;
} else {
console.log("Other error - " + error);
}
}
console.log(a); // -> -2
a = 10;
try {
a = b; // ReferenceError
} catch (error) {
console.log("An Error!"); // -> An Error!
} finally {
console.log("Finally!"); // -> Finally!
}
console.log(a); // -> 10
console.log("start"); // -> start
try {
throw 100;
} catch (error) {
console.log(error); // -> 100
}
console.log("end"); // -> end
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
console.log(100 / 0); // -> Infinity
console.log(100 * "2"); // -> 200
console.log(100 * "abc"); // -> NaN
console.log(Math.pow("abc", "def")); // -> NaN
let sX = "x";
let sY = 11;
let x = Number(sX);
let y = Number(sY);
if (Number.isFinite(x) && Number.isFinite(y) && y !== 0) {
console.log(x / y);
} else {
console.log("incorrect arguments");
}