Skip to content

Commit f87bdb5

Browse files
committed
Try / Catch
1 parent 6055b0a commit f87bdb5

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

app.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ const url = "./api/people.json";
77
const btn = document.querySelector(".btn");
88

99
btn.addEventListener("click", async () => {
10-
const response = await fetch(url);
11-
const data = await response.json();
12-
displayPeople(data);
10+
try {
11+
const response = await fetch(url);
12+
const data = await response.json();
13+
displayPeople(data);
14+
} catch (error) {
15+
console.log(error);
16+
}
1317
});
1418

1519
const displayPeople = (people) => {
@@ -23,3 +27,15 @@ const displayPeople = (people) => {
2327
element.innerHTML = displayData;
2428
document.body.appendChild(element);
2529
};
30+
31+
// console.log(random);
32+
33+
// Here "catch" will not block running the app like the above console statement does as random is not defined
34+
try {
35+
console.log("hello");
36+
console.log(random);
37+
} catch (error) {
38+
console.log(error);
39+
}
40+
41+
console.log("hello");

0 commit comments

Comments
 (0)