Javascript 我在一些遗留代码中看到了几个catch块。这有什么意义吗?

Javascript 我在一些遗留代码中看到了几个catch块。这有什么意义吗?,javascript,try-catch,Javascript,Try Catch,在我当前的项目中,我看到了一些奇怪的代码结构,如下所示: async_api_call_with_throw_errors //pseudocode .then(() => async_call_with_throw_errors) //pseudocode .then(() => async_call_with_throw_errors) //pseudocode .then(() => async_call_with_throw_errors)

在我当前的项目中,我看到了一些奇怪的代码结构,如下所示:

  async_api_call_with_throw_errors //pseudocode
    .then(() => async_call_with_throw_errors) //pseudocode
    .then(() => async_call_with_throw_errors) //pseudocode
    .then(() => async_call_with_throw_errors) //pseudocode
    .then(() => async_call_with_throw_errors) //pseudocode
    .catch((e) => {
        Sentry.captureException(e)
        if (e?.validateError) {
         //some actions
        } else {
          dispatch({ type: errorOccurred, errorText: e?.message, fatalError: false })
        }
      })
    .catch(e => {
      Sentry.captureException(e)
      dispatch({ type: errorOccurred, errorText: e, fatalError: false })
    })
若catch块捕获多个错误类型,这是正常的,但两个catch块之后有相同的错误,那个么这个块有意义吗


我想不是。但是也许我不知道一些重要的关于尝试…捕获的东西?

捕获的东西不适合所有相同的尝试。最后一个捕获是try块。但第一个陷阱是承诺链

try {
-------
.catch(e => {
  Sentry.captureException(e)
  dispatch({ type: errorOccurred, errorText: e, fatalError: false })
})
承诺块是

async_api_call_with_throw_errors //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.catch((e) => {
    const eventId = Sentry.captureException(e)
    if (e?.validateError) {
     //some actions
    } else {
      dispatch({ type: errorOccurred, errorText: e?.message, fatalError: false })
    }
  })
现在,我们不应该在异步调用中使用try/catch。如果您想在异步操作周围使用try/catch块,那么应该使用wait来调用异步方法,并且不应该有承诺解析链。代码应该写为

try {
  await async_api_call_with_throw_errors;
  await async_api_call_with_throw_errors;
  await async_api_call_with_throw_errors; 
    .....
} catch (error) {
   handle errors
}

顺便说一句,您应该确保要使用async/await的then函数也应该是async函数。

捕获不适用于所有相同的尝试。最后一个捕获是try块。但第一个陷阱是承诺链

try {
-------
.catch(e => {
  Sentry.captureException(e)
  dispatch({ type: errorOccurred, errorText: e, fatalError: false })
})
承诺块是

async_api_call_with_throw_errors //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.catch((e) => {
    const eventId = Sentry.captureException(e)
    if (e?.validateError) {
     //some actions
    } else {
      dispatch({ type: errorOccurred, errorText: e?.message, fatalError: false })
    }
  })
现在,我们不应该在异步调用中使用try/catch。如果您想在异步操作周围使用try/catch块,那么应该使用wait来调用异步方法,并且不应该有承诺解析链。代码应该写为

try {
  await async_api_call_with_throw_errors;
  await async_api_call_with_throw_errors;
  await async_api_call_with_throw_errors; 
    .....
} catch (error) {
   handle errors
}

顺便说一下,您应该确保要使用async/await的then函数也应该是一个异步函数。

如果前一个catch抛出错误,而后一个catch捕获到错误,则具有多个catch可以工作。 看看下面的代码

const promise1 = new Promise((resolve, reject) => {
  throw 'Uh-oh!';
});

promise1.catch((error) => {
  console.error(error);
  throw "Error was caught!"
}).catch(err => {
    console.error(err)
})
// Expect Output:
//  > Uh-oh!
//  > Error was caught!

希望这能消除一些疑问。

如果前一个捕获抛出错误,然后被下一个捕获捕获,那么多个捕获可以起作用。 看看下面的代码

const promise1 = new Promise((resolve, reject) => {
  throw 'Uh-oh!';
});

promise1.catch((error) => {
  console.error(error);
  throw "Error was caught!"
}).catch(err => {
    console.error(err)
})
// Expect Output:
//  > Uh-oh!
//  > Error was caught!

希望这能消除一些疑问。

如果第一次捕获抛出错误,它应该会起作用,尽管我不确定为什么会有人这样做。

如果第一次捕获抛出错误,它应该会起作用,尽管我不确定为什么会有人这样做。

你确定他们都是同一次尝试的一部分吗?哦一开始你没有尝试过你确定他们都是同一次尝试的一部分吗?哦一开始你并不担心这件事,但一开始就没有尝试障碍。我的错。我编辑代码:很抱歉,开头没有try块。我的错。我编辑代码: