I have a simple use case that I want to execute a statement inside the catch blog inside catch block of calling function. Somehow in my code there is no execution of catch block statement inside calling function
async function main() {
try {
console.log('jatin')
const hello = await second()
console.log(hello) //ok
console.log('yes boy') //ok
} catch (e) {
console.log('cm') //not ok
console.log(e) //not ok
}
}
async function second() {
try {
console.log('in second') //ok
throw new Error()
} catch (error) {
console.log('trial') //ok
console.log(error) //ok
return error
}
}
main()
>Solution :
You already caught the Error
in the second
function. If you want it to propagate back to the caller, you would need to rethrow it (or don’t catch it in the first place).
async function main() {
try {
console.log('main');
const hello = await second();
console.log(hello);
} catch (e) {
console.log('catch block of main');
console.log(e);
}
}
async function second() {
try {
console.log('second');
throw new Error();
} catch (error) {
console.log('catch block of second');
console.log(error);
throw error;
}
}
main();