如何识别自定义错误 [英] How can I identify custom error

查看:74
本文介绍了如何识别自定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为自定义错误分配代码/ ID:

I need to assigne an code/id to my custom error:

这是我创建错误的时候:

This is when I create the error:

var err=new Error('Numero massimo di cambi di username raggiunto');

有谁帮我理解我怎么做?

Anyone to help me to understand how I can do this?

推荐答案

定义

function MyError(code, message) {
  this.code = code;
  this.message = message;
  Error.captureStackTrace(this, MyError);
}
util.inherits(MyError, Error);
MyError.prototype.name = 'MyError';






提高


Raise

throw new MyError(777, 'Smth wrong');






Catch


Catch

if (err instanceof MyError) {
  console.log(err.code, err.message);
}

这篇关于如何识别自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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