Open gracekrcx opened 1 year ago
const getLocalStorage = name => {
try {
return JSON.parse(localStorage.getItem(name))
} catch (e) {
console.error(e)
return false
}
}
const res = getLocalStorage('products')
// console.log(!!res, res, typeof res)
// console.log(Array.isArray(res))
const products = Array.isArray(res) ? res : []
Control flow and error handling
function f() {
try {
throw "bogus";
} catch (e) {
console.log('caught inner "bogus"');
// This throw statement is suspended until
// finally block has completed
throw e;
} finally {
return false; // overwrites the previous "throw"
}
// "return false" is executed now
}
try {
console.log(f());
} catch (e) {
// this is never reached!
// while f() executes, the `finally` block returns false,
// which overwrites the `throw` inside the above `catch`
console.log('caught outer "bogus"');
}
// Logs:
// caught inner "bogus"
// false
The JavaScript exceptions thrown by JSON.parse() occur when string failed to be parsed as JSON.
Proper way to catch exception from JSON.parse Unexpected end of JSON input Error in JavaScript SyntaxError: JSON.parse: bad parsing