-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.js
168 lines (149 loc) · 4.52 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
160
161
162
163
164
165
166
167
168
// Day 12: Error Handling
// Activity 1: Basic Error Handling with Try-Catch
// • Task 1: Write a function that intentionally throws an error and use a try-catch block to handle the error and log an appropriate message to the console.
const add = (a,b) =>{
const sum = Number(a+b);
if(isNaN(sum)){
throw new Error("sum is not a number");
}
else{
console.log(sum);
}
}
try {
sum(2,'a');
}
catch (error) {
console.log(error.message);
}
// • Task 2:Create a function that divides two numbers and throws an error if the denominator is zero. Use a try-catch block to handle this error.
const divide = (a,b) => {
if(b==0){
throw new Error("denominator is zero");
}
console.log(a/b);
}
try{
divide(2,0)
} catch(error){
console.log(error.message);
}
// Activity 2: Finally Block
// • Task 3: Write a script that includes a try-catch block and a finally block. Log messages in the try, catch, and finally blocks to observe the execution flow.
function trycatchfinally(){
try{
divide(2,0);
console.log("Inside Try block");
}
catch(error){
console.log(error.message,",Inside Catch block");
}
finally{
console.log("Inside Finally block");
}
}
trycatchfinally();
// Activity 3: Custom Error Objects
// • Task 4 : Create a custom error class that extends the built-in Error class. Throw an instance of this custom error in a function and handle it using a try-
// catch block.
class CustomError extends Error{
constructor(message){
super(message);
this.name = 'CustomError'
}
}
function throwError(){
throw new CustomError('This is a custom error message')
}
try{
throwError();
}
catch(error){
if(error instanceof CustomError){
console.error('Caught a custom error:', error.message);
}
else{
console.error('Caught an unexpected error:', error);
}
}
// • Task 5: Write a function that validates user input (e.g., checking if a string is not empty) and throws a custom error if the validation fails. Handle the
// custom error using a try-catch block.
class myError extends Error{
constructor(message){
super(message);
this.name = 'myError'
}
}
function checkInput(name){
if(name.length === 0){
throw new myError("Name is empty");
}
}
try{
checkInput("")
}
catch(error){
if(error instanceof myError){
console.log('Caught a custom error:', error.message);
} else{
console.error('Caught an unexpected error:', error);
}
}
// Activity 4: Error Handling in Promises
// • Task 6: Create a promise that randomly resolves or rejects. Use . catch() to handle the rejection and log an appropriate message to the console.
const randomPromise = new Promise((resolve,reject) => {
const randomNumber = Math.random()
if(randomNumber > 0.5){
resolve("Promise resolved successfully")
} else{
reject("Promise rejected")
}
})
randomPromise
.then((message) => {
console.log(message);
})
.catch((error)=>{
console.log('Caught an error:',error);
})
// • Task 7: Use try-catch within an async function to handle errors from a promise that randomly resolves or rejects, and log the error message.
async function trycatch(){
try{
const msg = await randomPromise;
console.log(msg);
}
catch(error){
console.log('Caught an error:',error);
}
}
trycatch();
// Activity 5: Graceful Error Handling in Fetch
// • Task 8: Use the fetch API to request data from an invalid URL and handle the error using . catch() . Log an appropriate error message to the console.
fetch("hjksldjhib")
.then(response => {
if(!response.ok){
throw new Error('Network response not ok')
}
return response.json()
})
.then(data=>{
console.log(data);
})
.catch(error=>{
console.error('Fetch error:',error.message);
})
// Task 9: Use the fetch API to request data from an invalid URL within an async function and handle the error using try-catch. Log an appropriate error message.
async function fetchData() {
try {
const response = await fetch('https://invalid-url.com/data');
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);
}
}
// Call the async function to demonstrate its functionality
fetchData();