-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter4-ErrorHandling.html
98 lines (78 loc) · 1.97 KB
/
chapter4-ErrorHandling.html
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
<html>
<body>
<script type="text/javascript">
// Error Handling
//Returning a special value
function between(string, start, end) {
var startAt = string.indexOf(start);
if (startAt == -1)
return undefined;
startAt += start.length;
var endAt = string.indexOf(end, startAt);
if(EndAt == -1)
return undefined;
return string.slice(startAt,endAt);
}
var input = promprt("Tell me something: ");
var parenthesized = between(input, "(", ")",);
if (parenthesized != undefined)
print("You parenthesized '", parenthesized, "' .");
function lastElement(array) {
if (array.length > 0)
return array[array.length - 1];
else
return undefined;
}
lastElement([1,2,undefined]);
// was there a last element? Undefined is returned, so you don't know
// Exceptions
function lastElement(array) {
if (array.length > 0)
return array[array.length - 1];
else
throw "Cannot take the last element of an empty array.";
}
function lastElementPlusTen(array) {
return lastElement(array) +10;
}
try {
print(lastElementPlusTen([]));
}
catch(error) {
print("Something went wrong: ", error);
}
//Clean up after exceptions with the "finally" block
function processThing(thing) {
var prevThing = currentThing;
currentThing = thing;
try {
// do complicated thing that might throw exception
}
finally {
currentThing=prevThing;
}
}
// Error objects
//as seen earlier, the "error" can be thrown
// or you can manually throw one
throw new Error("Wolf!");
//Selective Catching
var InvalidInputError = new Error("Invalid Numeric Input");
function inputNumber() {
var input = Number(prompt("Give me a number: ", ""));
if (isNaN(input))
throw InvalidInputError;
return input;
}
try {
alert(inputNumber() + 5);
break;
}
catch(e) {
if(e != InvalidInputError)
throw e;
alert("You did not input a number. Try again.");
}
</script>
</body>
</html>