-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorHandling.js
79 lines (60 loc) · 1.34 KB
/
ErrorHandling.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
//***************************ERROR HANDLING ********************** */
//SYNTAX
/*
try
{
// code that may throw an error
}
catch(ex)
{
// code to be executed if an error occurs
}
finally{
// code to be executed regardless of an error occurs or not
}
*/
try
{
throw "Error in try block";
}
catch(error)
{
console.error("The error is : ",error);
}
//we can throw an OBJECT if error occurs
try
{
throw {
number: 101,
message: "Error occurred"
};
}
catch (error) {
console.error(`${error.number}- ${error.message}`);
}
//Error object is thrown when error is occured and it has two properties
//1. error.name
//2. error.message
try{
var a= [3,4,5]; //a is an array
console.log(a); // displays elements of a
console.log(b); //b is undefined but still trying to fetch its value. Thus catch block will be invoked
}
catch(e)
{
console.log(`The error generated is : ${e.message}`); //Handling error
}
//A finally block contains all the crucial statements that must be executed whether exception occurs or not.
try{
var a=2;
if(a==2)
console.log("True");
}
catch(Error)
{
console.log(`Error found :${e.message}`);
}
finally
{
console.log("Value of a is 2 ");
}