-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_12.js
167 lines (126 loc) · 3.11 KB
/
day_12.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
// Activity 1: Basic Error Handling with Try-Catch
// Task 1
const task1Func = () => {
try {
data == 1;
} catch (err) {
console.log(`Error occured in task 1 - ${err.message}`);
}
}
task1Func();
// Task 2
const num = 8;
const den = 0;
const task2Func = (num, den) => {
try {
if(den > 0) {
console.log(num/den);
}
else {
throw new Error(`Denominator can not be zero`);
}
} catch (error) {
console.log(`Error occured in task 2 - ${error.message}`);
}
}
task2Func(num, den);
// Activity 2: Finally Block
// Task 3
const task3Func = () => {
try {
console.log("Inside the try block")
} catch (error) {
console.log("Inside the cath block")
} finally {
console.log("Inside the finally block")
}
}
task3Func();
// Activity 3: Custom Error Objects
// Task 4
const age = 12;
const task4Func = (age) => {
try {
if(age < 18) {
throw new Error(`Get lost you are not eligible to drink`)
}
console.log("Sure, Here is your drink! Enjoy")
} catch (error) {
console.log(`Error occured in task 4 - ${error}`)
}
}
task4Func(age);
// Task 5
const _name = "";
const greet = (name) => {
try {
if(name.trim().length === 0) {
throw new Error(`No name is provided, Please enter your name correctly!`)
}
else {
console.log(`Hi ${name}, have a great day ahead ♥`)
}
} catch (error) {
console.log(`Error occured in task 5 - ${error.message}`);
}
}
greet(_name);
// Activity 4: Error Handling in Promises
// Task 6
const task6promise = new Promise((resolve, reject) => {
const randNum = Math.floor(Math.random() * 10);
if(randNum > 5) {
resolve(randNum);
}
else {
reject(`Promise rjected as the randNum <= 5, Please try again`);
}
}).then(data => {
console.log(data);
}).catch(err => {
console.log(`Error occured in task 6 - ${err}`)
})
// Task 7
const task7Func = async (msg, response) => {
try {
if(response == 1) {
console.log(msg);
}
else {
throw new Error(msg);
}
} catch (e) {
console.log(`Error occured in task 7 - ${e.message}`)
}
}
const task7promise = new Promise((resolve, reject) => {
const randNum = Math.floor(Math.random() * 10);
if(randNum > 5) {
resolve(randNum);
}
else {
reject(`Promise rjected as the randNum <= 5, Please try again`);
}
}).then(data => {
task7Func(data, 1);
}).catch(err => {
task7Func(err, 0);
})
// Activity 5: Graceful Error Handling in Fetch
// Task 8
const data = fetch("https://api.com/")
.catch(error =>
console.log(`Error occured in task 8 - ${error.message}`)
);
// Task 9
const task9Func = async () => {
try {
const data = await fetch("https://api.restful-api.dev/objects/-1");
const response = await data.json();
console.log(response);
}
catch(err) {
console.log(`Error occured in task 9 - ${err}`);
}
}
task9Func();