如何抛出捕获块?

2guxujil  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(421)

我目前正在研究如何从随后捕获的块中抛出异常。我想进入到捕集器里面 errorHandler() 功能。

const errorHandler = function () {
  try {
    thisFunctionReturnsAnError().then(response => {
      console.log(response);
    });
  } catch (e) {
    console.log(e); //How to trigger this?
  }
};

const thisFunctionReturnsAnError = function () {
  return3()
    .then(value => {
      throw new Error('This is the error message.');
    })
    .catch(err => {
      //return Promise.reject('this will get rejected');
      throw err;
      //this one should somehow got to the catch that is located in the errorHandler() function. How to do this?
      //I know that this 'err' will be in the next catch block that is written here. This is not what i want.
    });
};

const return3 = async function () {
  return 3;
};

errorHandler();

我在stackoverflow上搜索了一会儿,但没有任何帮助。我相信这个问题经常被问到,但我找不到答案,对此我深表歉意。
编辑:在这里添加了另一个版本的代码,但仍然不起作用

const errorHandler = async function () {
  try {
    thisFunctionReturnsAnError()
      .then(response => console.log(response))
      .catch(err => console.log(err));
  } catch (e) {
    //console.log(e); //How to trigger this?
  }
};

const thisFunctionReturnsAnError = function () {
  return3()
    .then(value => {
      throw new Error('This is the error message.');
    })
    .catch(err => {
      return Promise.reject(`Message is: ${err}`);
    });
};

const return3 = async function () {
  return 3;
};

errorHandler();

我将得到以下错误消息:uncaught(in promise)消息是:error:这是错误消息。

np8igboo

np8igboo1#

无法执行代码,因为“thisfunctionreturnsanerror”未返回承诺。这意味着您不能对返回值调用“then”。

thisFunctionReturnsAnError().then(response => { // will not work

为什么不总是用承诺呢?

const errorHandler = function () {
  thisFunctionReturnsAnError()
    .then((response) => {
      console.log(response);
    })
    .catch((error) => {
      console.log('errorHandler: Handle the error.');
    });
};

const thisFunctionReturnsAnError = function () {
  return return_Three()
    .then((value) => {
      throw new Error('This is the error message.');
    })
    .catch((err) => {
      //return Promise.reject('this will get rejected');
      throw err;
      //this one should somehow got to the catch that is located in the errorHandler() function. How to do this?
      //I know that this 'err' will be in the next catch block that is written here. This is not what i want.
    });
};

const return_Three = async function () {
  return 3;
};

errorHandler();

/*****JUST ANOTHER SYNTAX*******/
const secondErrorHandler = async function () {
  try {
    await thisFunctionReturnsAnError();
  } catch (error) {
    console.log('secondErrorHandler: Handle the error.');
  }
};

secondErrorHandler();
avwztpqn

avwztpqn2#

您无法使用synchronous处理承诺拒绝 try / catch . 不要用它,用承诺 .catch() 你喜欢什么样的方法 thisFunctionReturnsAnError 功能。
您可以用以下方式处理拒绝承诺: try / catch 使用时 async / await 语法(在 return3 ):

async function errorHandler() { /*
^^^^^ */
  try {
    const response = await thisFunctionReturnsAnError();
//                   ^^^^^
    console.log(response);
  } catch (e) {
    console.log(e); // works
  }
}

相关问题