-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.js
120 lines (103 loc) · 2.04 KB
/
day12.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
//Bsic Error handling with Try-Catch
try{
console.log("executing...");
throw("Error occured!")
}
catch(msg){
console.error(msg)
}
let devide=(x,y)=>{
try{
if(y==0)
throw("Cannot devide by Zero!");
return x/y;
}
catch(msg){
console.error(msg)
return ""
}
}
console.log(devide(20,10))
console.log(devide(20,0))
//Finnaly Block
try{
console.log("This is try Block");
throw("This is a error msg")
}
catch(msg){
console.error(msg)
console.log("This is a catch block")
}
finally{
console.log("This is a finally block")
}
//Custom Error objects
class customError extends Error{
constructor(msg){
super(msg)
this.name="custom error"
}
}
function heyy(){
throw(new customError("this is a error"))
}
try{
heyy()
}
catch(e){
console.log(e.name+": "+e.message)
}
function validate(data){
if(data==""||data==undefined)
throw new customError("Validation Failed!!")
console.log(data)
}
try{
validate("heyey")
validate()
}
catch(err){
console.log(err.name+": "+err.message)
}
//Error handling in promises
new Promise((resolve,reject)=>{
if(Math.floor(Math.random()*2)==0)
reject("Error: Cannot fullfill the promise")
else
resolve("Promise resolved")
})
.then((data)=>{
console.log(data)
})
.catch((msg)=>{
console.error(msg)
});
(async()=>{
try{
await new Promise((resolve,reject)=>{
if(Math.floor(Math.random()*2)==0){
setTimeout(() => {
reject("Error: Cannot fullfill the promise")
}, 2999);
}
else
resolve("Promise resolved")
})
}
catch(msg){
console.error(msg)
}
})();
//Gracefull Error Handling in Fetch
fetch("invalidUrl")
.catch((err)=>{
console.log(err.code,": ",err.message)
});
(async()=>{
try{
let x=await fetch("hdskdkkd")
}
catch(err){
console.log(err.message)
}
})();