forked from wisac/javascript_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.-exceptions.js
182 lines (127 loc) · 4 KB
/
6.-exceptions.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
/*There are different error types in Javascript. These include:
1. EvalError - Occurs when eval() is used incorrectly.
2. RandgeError - Occurs when a number is outside range values
3. ReferenceError - Occurs when a reference type is expected at
a praticular location. Usually occurs when you try to access noexisting variable.
4. Syntax error - Occurs when there is error in syntax when
parsing the source code with eval()
TypeError - Occurs when variable is called with invalid type
5. URLError - Occurs when an error occurs when connecting to a
url
*/
//Use of try-catch statements
function createArray(length)
{
return new Array(length);
}
let array;
try
{
array = createArray(-5); //creating an array with a negative size. An error is expected
}
catch(error)
{
console.log(error.name); //log type of error: RangeError
console.log(error.message); //log error description: invalid array length
}
//THE ERROR OBJECT
/* The error object is available in the catch block provides two properties which can be used to get information about the error.
errorObject.name - type of error
errorObject.message - decription of the error
The error object also has fileName, lineNumber and columnName properties in the firefox environment. These are not standardized so using them is not advisable
The error object takes abituary names but this provide the properties.
*/
//USING TRY CATCH STATEMENTS
/* Syntax:
try
{
code to be executed
}
catch(error)
{
//
}
/* TRIGGERING ERRORS
The throw keyword is used to throw errors.
Syntax:
throw new ErrorType("Error message");
*/
function checkEven(evenNumber)
{
if (isNaN(parseFloat(evenNumber)))
{
throw new TypeError("A number must be entered");
}
if (evenNumber % 2 !== 0)
{
throw new Error("Number is not even");
}
return evenNumber;
}
// console.log(checkEven(8)) // 8
// console.log(checkEven(19)); // error: Number is not even
// console.log(checkEven("Kofi")); // TypeError: Even number not a number
/* NB: You can only use on catch block for every try block in javascript */
/* Errors and function call stack
When an error is thrown in a function, if a catch block is not found, the control jumps from that function to the function which called if a catch block is not found in that function too, the control jumps to the function that called it. This happens until the jump reaches the global code. If the error is not handled in the global code, it ends up with the user.
NB: Errors that can be handled or work around by the user should be forwarded to the user. Otherwise, it should be solved in other ways or written to the console.
*/
console.log("\n\nCall stack and error throwing");
function printA(a)
{
console.log("in A")
throw new TypeError("Error thrown in A");
}
function printB(b)
{
console.log("in B")
printA(4);
}
function printC(c)
{
console.log("In C")
printB(9);
return c;
}
function printD()
{
let p;
console.log("in D")
try
{
p = printC(3);
}
catch(error)
{
console.log(error.name);
console.log("Errror handled in D")// error in A handled here
}
return p * 5;
}
console.log(printD());
/*USING FINALLY STATEMENT
The finally block is alway executed whether the exception catch is handled or not.
*/
function tryCatchFinally()
{
try
{
let m = new Array(-6);
}
catch(er)
{
console.log(er.name);
console.log(er.message);
}
finally
{
console.log("done");
}
}
tryCatchFinally();
/*
various combinations
try - catch
try finally
try catch finally
*/