简体   繁体   中英

Can nested Try Catch trigger parent catch

I am logging some errors if an error occurs in the function and my code looks like this.

const startFunction = async () => {
 try {
  const A = await functionA();
  const B = await functionB();
  const C = await functionC();
 } catch {
  //LOG ERROR
 }
}

Each of these function has its own try-catch block. So my question is, if a catch block is triggered in one of the functions (A, B or C) will the parent's (ie startFunction) catch block also trigger. If not then is there a way to trigger it or do I have to log all functions individually.

The answer is NO in your current implementation.

If u want to populate the error from the inner functions to the top level catch block, you need to re throw the error from the inner function catch block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM