-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.js
161 lines (126 loc) · 3.34 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
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
// Activity 1
// Task 1
function throwError() {
try {
throw new Error("This is an intentional error.");
} catch (error) {
console.error("Caught an error:", error.message);
}
}
throwError();
// Task 2
function divideNumbers(a, b) {
try {
if (b === 0) {
throw new Error("Cannot divide by zero.");
}
return a / b;
} catch (error) {
console.error("Error:", error.message);
}
}
divideNumbers(10, 2);
divideNumbers(10, 0);
// Activity 2
// Task 3
function exampleWithFinally() {
try {
console.log("In try block.");
throw new Error("An error occurred.");
} catch (error) {
console.error("Caught an error:", error.message);
} finally {
console.log("Finally block executed.");
}
}
exampleWithFinally();
// Activity 3
// Task 4
class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}
function throwCustomError() {
try {
throw new CustomError("This is a custom error.");
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}
}
throwCustomError();
// Task 5
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
}
function validateInput(input) {
try {
if (input.trim() === "") {
throw new ValidationError("Input cannot be empty.");
}
console.log("Input is valid:", input);
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}
}
validateInput(" ");
validateInput("Valid Input");
// Activity 4
// Task 6
function randomPromise() {
return new Promise((resolve, reject) => {
const success = Math.random() > 0.5;
if (success) {
resolve("Promise resolved successfully.");
} else {
reject(new Error("Promise rejected."));
}
});
}
randomPromise()
.then((message) => console.log(message))
.catch((error) => console.error("Caught an error:", error.message));
// Task 7
async function asyncFunctionWithErrorHandling() {
try {
const message = await randomPromise();
console.log(message);
} catch (error) {
console.error("Caught an error in async function:", error.message);
}
}
asyncFunctionWithErrorHandling();
// Activity 5
// Task 8
function fetchData(url) {
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok.");
}
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error("Fetch error:", error.message));
}
fetchData("https://jsonplaceholder.typicode.com/posts/1"); // Valid URL
fetchData("https://invalid-url"); // Invalid URL
// Task 9
async function fetchDataWithAsync(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Network response was not ok.");
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error("Fetch error:", error.message);
}
}
fetchDataWithAsync("https://jsonplaceholder.typicode.com/posts/1"); // Valid URL
fetchDataWithAsync("https://invalid-url"); // Invalid URL