You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/how-can-you-create-custom-error-objects/en-US.mdx
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: How can you create custom error objects?
6
6
7
7
To create custom error objects in JavaScript, you can extend the built-in `Error` class. This allows you to add custom properties and methods to your error objects. Here's a quick example:
8
8
9
-
```js
9
+
```js live
10
10
classCustomErrorextendsError {
11
11
constructor(message) {
12
12
super(message);
@@ -43,7 +43,7 @@ class CustomError extends Error {
43
43
44
44
You can add custom properties to your custom error class to provide more context about the error.
45
45
46
-
```js
46
+
```js live
47
47
classCustomErrorextendsError {
48
48
constructor(message, errorCode) {
49
49
super(message);
@@ -65,7 +65,7 @@ try {
65
65
66
66
You can also add custom methods to your custom error class to handle specific error-related logic.
67
67
68
-
```js
68
+
```js live
69
69
classCustomErrorextendsError {
70
70
constructor(message, errorCode) {
71
71
super(message);
@@ -89,7 +89,15 @@ try {
89
89
90
90
You can use the `instanceof` operator to check if an error is an instance of your custom error class.
91
91
92
-
```js
92
+
```js live
93
+
classCustomErrorextendsError {
94
+
constructor(message, errorCode) {
95
+
super(message);
96
+
this.name='CustomError';
97
+
this.errorCode= errorCode;
98
+
}
99
+
}
100
+
93
101
try {
94
102
thrownewCustomError('This is a custom error message', 404);
0 commit comments