try catch in javascript
try {
// Try to run this code
}
catch(err) {
// if any error, Code throws the error
}
finally {
// Always run this code regardless of error or not
//this block is optional
}
javascript try
var someNumber = 1;
try {
someNumber.replace("-",""); //You can't replace a int
} catch(err) {
console.log(err);
}
try catch js
async function getData() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
const data = await response.json();
const { userId, id, title, completed } = data;
console.log(userId, id, title, completed);
} catch(err) {
alert(err)
}
}
getData();
try catch js
try {
// Try to run this code
}
catch(err) {
// if any error, Code throws the error
}
finally {
}
javascript try catch
Exception Handling
|