-
Notifications
You must be signed in to change notification settings - Fork 0
/
try-catch.js
44 lines (31 loc) · 1.13 KB
/
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
// try-catch : GRACEFUL HANDLING OF ERRORS
// note: a better approach is 'promises',
// like in the case of handling web requests
// a scenario
const convToINR = (_USD) => _USD * 64;
console.log(convToINR(5)); // -> works fine
console.log(convToINR('5')); // -> still works fine
console.log(convToINR('five')); // -> prints NaN, bad
// this last case needs to be handled inside the function.
// fn convToINR v2
const convToINR_v2 = (_USD) => {
if (typeof _USD === 'number')
return _USD * 64;
else
throw Error('Amount not in numbers!');
};
// note: interface errors usually occurs when we mistype sth
/*
// the error here freezes the program flow,
// the error is shown and then the program
// stops
console.log(convToINR_v2('five')); // -> generates error
console.log('This line not printed if the program crashes above.'); // -> not printed
*/
// try-catch ensures that the progra does not freeze.
try {
console.log(convToINR_v2('five'));
} catch (error) {
console.log(error.message);
}
console.log('This line not printed if the program crashes above.'); // -> gets printed!