You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is not how I would use "try catch and fetch." In fact, both of the mentioned problems are avoided by properly using "try catch" and the "return" in the appropriate places. Furthermore, in those examples, the code becomes longer with the solution since multiple if statements are required for different errors.
Could you provide a real example of nested try catches that cannot be resolved with a single try catch and where the proposed solution is clearer? It would be helpful to understand the idea.
Thank you very much. Best regards.
The text was updated successfully, but these errors were encountered:
// Nests 1 level for each error handling block
async function readData(filename) {
try {
const fileContent = await fs.readFile(filename, "utf8")
} catch (error) {
handleFileError(error)
return
}
}
// Declares reassignable variables outside the block, which is undesirable
async function readData(filename) {
let fileContent
let json
try {
fileContent = await fs.readFile(filename, "utf8")
} catch (error) {
handleFileError(error)
return
}
try {
json = JSON.parse(fileContent)
} catch (error) {
handleJsonError(error)
return
}
return json.data
}
Good morning.
This is not how I would use "try catch and fetch." In fact, both of the mentioned problems are avoided by properly using "try catch" and the "return" in the appropriate places. Furthermore, in those examples, the code becomes longer with the solution since multiple if statements are required for different errors.
Could you provide a real example of nested try catches that cannot be resolved with a single try catch and where the proposed solution is clearer? It would be helpful to understand the idea.
Thank you very much. Best regards.
The text was updated successfully, but these errors were encountered: