如何将函数内部的捕获错误传递给父级 [英] How to pass catch error inside function into parent

查看:27
本文介绍了如何将函数内部的捕获错误传递给父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这几行代码示例,想知道下面的逻辑到底是怎样的:

I have this sample couple lines of code, wanna know how exactly the logic below:

try {
  var response = child()
  console.log('why here')
} catch (err) {
  console.log('should show this', err)
}

function child() {
  try {
    throw new Error('oops');
  } catch (err) {
    console.log('child error', err)
  }
}

预期结果是console should show this with err, 但它返回console why here

The expected result is to console should show this together with err, but it returned console why here

推荐答案

你只是再次抛出同样的错误.catch 允许你处理它,你想记录它,或者阻止它等等.或者你可以重新抛出它.当你 throw new Error() 时.您正在创建一个错误的新实例.投掷是什么使它能够做它所做的.但是你可以在不抛出错误的情况下声明错误.

You merely throw the same error again. The catch allows you to handle it, you want to log it, or prevent it and etc. Or you can just rethrow it. When you throw new Error(). You're creating a new instance of an Error. The throw is what makes it able to do what it does. But you can declare Errors without throwing them.

let e = new Error('我的错误:D');//做一些事情......扔e;

如果你抛出 e 并且它被捕获,e 仍然是一个错误实例.您可以一次又一次地抛出它,直到应用程序崩溃:D

if you throw e and it's caught, e is still an instance of an error. You can just throw it again and again until the app crashes :D

但这是记录有关堆栈跟踪、局部变量等详细信息的好方法.大多数专业团队都有自己的自定义控制台记录器,允许将控制台日志保存到数据库中并在以后查看.此日志事件可能会在每次尝试捕获时发生,并将次要细节添加到日志中,这将有助于调试庞大而复杂的项目.此外,在许多强类型语言中,您可以有多个捕获,根据错误类型(ReferenceError、TypeError 等)允许不同的响应.因此,您可以捕获一个错误,分析它发生的原因,并可能抛出您自己的自定义错误,以便更高级别的进程相应地处理它:o

But it's a good way to log details about the stack trace, local variables and etc. Most professional teams will have their own custom console logger that allows the console logs to be saved to a data base and reviewed later. This logging event might occur at every try catch and add minor details to the log that'll help with debugging a massive and complicated project. Additionally, in a lot strongly typed languages, you can have multiple catches that allow different responses depending on the error type (ReferenceError, TypeError, etc). So you can catch one error, analyze why it happened and maybe throw your own custom error so that a higher up process handles it accordingly :o

try 
{
  var response = child()
  console.log('why here')
} 
catch (err) 
{
  console.log('should show this', err)
}

function child() 
{
  try 
  {
    throw new Error('oops');
  } 
  catch (err) 
  {
    console.log('child error', err)
    throw err;
  }
}

通过 Javascript 中的类型处理错误的一种方法

有更好的方法可以做到这一点,但这只是一个例子.想象一下,如果我们有一个为每个 switch case 触发的函数,而不仅仅是一个控制台日志.根据错误类型,启动差异响应.如果您在 Error 类中有多个继承层,则会出现此问题.

There are better ways to do this, but this is just an example. Imagine if we had a function that fired off for each switch case instead of just a console log. Depending on the type of error, a difference response is initiated. A problem that occur with this is if you have multiple layers of inheritance within Error classes.

function ValidateClassType(obj, classDef)
{
  if(!obj || !classDef)
  {
    throw new ReferenceError("1 or More Parameters is Undefined");
  }
  if(typeof classDef != 'function')
  {
    throw new TypeError("classDef Must be a Callable Constructor.")
  }
  
  let isInstanceOf = obj instanceof classDef;
  if(!isInstanceOf)
  {
    throw new TypeError("Invalid Class Type for Operation");
  }
}

try
{
  ValidateClassType(1, null);
}
catch(err)
{
  let errType = err.constructor.name;
  switch(errType)
  {
    case 'ReferenceError':
      console.log('A Reference Error Occured.');
      break;
    case 'TypeError':
      console.log('A type Error Occured.');
      break;
    default:
      console.log('An Unhandled Error Occurred.');
  }
}

这篇关于如何将函数内部的捕获错误传递给父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆