首页 > 网站 > WEB开发 > 正文

JavaScript Patterns 3.8 Error Objects

2024-04-27 14:23:01
字体:
来源:转载
供稿:网友

javaScript Patterns 3.8 Error Objects

2014-06-05 23:56 by 小郝(Kaibo Hao), ... 阅读, ... 评论, 收藏, 编辑

The error objects created by constructors(Error(), SyntaxError(), TypeError(), and others) have the following PRoperties:

name

The name property of the constructor function that created the object; it could be the general “Error” or a more specialized constructor such as “RangeError”.

message

The string passed to the constructor when creating the object.

You can be creative when it comes to your custom error objects and use them to restore the application state back to normal.

try {    // something bad happened, throw an error    throw {        name: "MyErrorType", // custom error type        message: "oops",        extra: "This was rather embarrassing",        remedy: genericErrorHandler // who should handle it    };} catch (e) {    // inform the user    alert(e.message); // "oops"    // gracefully handle the error    e.remedy(); // calls genericErrorHandler()}

上一篇:仅有 265 行的第一人称引擎

下一篇:HteOS

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表