如何正确丢失属性的错误? [英] How to throw error properly for a missing property?

查看:91
本文介绍了如何正确丢失属性的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    if (win.NS && win.NS.molist && win.NS.molist.utility) {
        NS = win.NS;
        NS.molist.dom = true;
    } else {
        throw "dom requires utility module";
    }

对于上面的代码段,抛出错误的正确方法是什么? win.NS.molist.utility 不存在?

For the snippet above, what is the proper way to throw an error, if win.NS.molist.utility does not exist?

我可以把我想要显示的文本在调试器中显示?

Can I just throw up the text I want displayed to be shown in the debugger?

也许,我应该使用内置的错误类型之一?

Perhaps, I should use one of the built in error types?

也许一个新的TypeError ,我不知道有很多全局错误对象。

Maybe a new TypeError, I'm not sure as there are many global Errors objects.

推荐答案

您可以 throw 完全自定义错误

function ModuleNotFoundError(message) {
  this.name = "ModuleNotFound";
  this.message = message || "dom does not have this module";
}
ModuleNotFoundError.prototype = new Error();
ModuleNotFoundError.prototype.constructor = ModuleNotFoundError;

throw new ModuleNotFoundError('win.NS.molist.utility');

这篇关于如何正确丢失属性的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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