我正在尝试自定义 javascript 中异常抛出的错误消息。
到目前为止我的尝试都失败了。
我正在尝试这个,但它不起作用
function abc(sender, args) {
alert("ex2");
throw ("error new");
alert("");
}
你需要throw
and object
(with name and message属性)而不是string
生成自定义exception
:
function abc(sender, args) {
alert('ex2');
throw {
name: 'YourErrorName',
message: 'YourErrorMessage'
};
alert('');
}
以下是如何引发事件并捕获事件的示例exception
其中包含您的姓名和消息值:
function triggerError(){
throw {
name: 'YourErrorName',
message: 'YourErrorMessage'
};
};
(function cathError(){
try {
triggerError();
}
catch(e){
console.log(e.message);
console.log(e.name);
}
}());
看看这个codepen http://codepen.io/ianaya89/pen/bNqBvr看看它是否有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)