如何抛出捕获块?

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

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

  1. const errorHandler = function () {
  2. try {
  3. thisFunctionReturnsAnError().then(response => {
  4. console.log(response);
  5. });
  6. } catch (e) {
  7. console.log(e); //How to trigger this?
  8. }
  9. };
  10. const thisFunctionReturnsAnError = function () {
  11. return3()
  12. .then(value => {
  13. throw new Error('This is the error message.');
  14. })
  15. .catch(err => {
  16. //return Promise.reject('this will get rejected');
  17. throw err;
  18. //this one should somehow got to the catch that is located in the errorHandler() function. How to do this?
  19. //I know that this 'err' will be in the next catch block that is written here. This is not what i want.
  20. });
  21. };
  22. const return3 = async function () {
  23. return 3;
  24. };
  25. errorHandler();

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

  1. const errorHandler = async function () {
  2. try {
  3. thisFunctionReturnsAnError()
  4. .then(response => console.log(response))
  5. .catch(err => console.log(err));
  6. } catch (e) {
  7. //console.log(e); //How to trigger this?
  8. }
  9. };
  10. const thisFunctionReturnsAnError = function () {
  11. return3()
  12. .then(value => {
  13. throw new Error('This is the error message.');
  14. })
  15. .catch(err => {
  16. return Promise.reject(`Message is: ${err}`);
  17. });
  18. };
  19. const return3 = async function () {
  20. return 3;
  21. };
  22. errorHandler();

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

np8igboo

np8igboo1#

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

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

为什么不总是用承诺呢?

  1. const errorHandler = function () {
  2. thisFunctionReturnsAnError()
  3. .then((response) => {
  4. console.log(response);
  5. })
  6. .catch((error) => {
  7. console.log('errorHandler: Handle the error.');
  8. });
  9. };
  10. const thisFunctionReturnsAnError = function () {
  11. return return_Three()
  12. .then((value) => {
  13. throw new Error('This is the error message.');
  14. })
  15. .catch((err) => {
  16. //return Promise.reject('this will get rejected');
  17. throw err;
  18. //this one should somehow got to the catch that is located in the errorHandler() function. How to do this?
  19. //I know that this 'err' will be in the next catch block that is written here. This is not what i want.
  20. });
  21. };
  22. const return_Three = async function () {
  23. return 3;
  24. };
  25. errorHandler();
  26. /*****JUST ANOTHER SYNTAX*******/
  27. const secondErrorHandler = async function () {
  28. try {
  29. await thisFunctionReturnsAnError();
  30. } catch (error) {
  31. console.log('secondErrorHandler: Handle the error.');
  32. }
  33. };
  34. secondErrorHandler();
展开查看全部
avwztpqn

avwztpqn2#

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

  1. async function errorHandler() { /*
  2. ^^^^^ */
  3. try {
  4. const response = await thisFunctionReturnsAnError();
  5. // ^^^^^
  6. console.log(response);
  7. } catch (e) {
  8. console.log(e); // works
  9. }
  10. }

相关问题