-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtry-catch-finally.js
More file actions
43 lines (34 loc) · 1.1 KB
/
try-catch-finally.js
File metadata and controls
43 lines (34 loc) · 1.1 KB
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
//: try-catch-finally Statement In Javascript
/*
//* The try-catch statement allow you to catch exception and handle them gracefull.
//* Sometimes, you want to execute a block whether exceptions occur or not
//* In this case, you can use the try-catch-finally statement with the following syntax
*/
try {
// try to run this code
} catch (error) {
// code to handle exceptions
} finally {
// Always run this code block regardless of error or not
//* this block is optional
}
const numerator = 100,
denominator = "a";
try {
console.log(nummerator / denominator); // NaN
console.log(a);
} catch (error) {
console.log("An error caught");
console.log("Error message" + error);
} finally {
console.log("Finally will execute every time");
}
/*
An error caught
Error messageReferenceError: nummerator is not defined
Finally will execute every time
*/
/*
=> Use the finally clause in the try-catch-finally statement to execute a block whether exceptions occur or not.
==> The try-catch-finally statement ignores the return statement in the try and catch blocks because the finally block always executes.
*/