Skip to content

Commit 33079ab

Browse files
committed
exec: how-can-you-create-custom-error-objects
1 parent dfbd09c commit 33079ab

File tree

1 file changed

+12
-4
lines changed
  • questions/how-can-you-create-custom-error-objects

1 file changed

+12
-4
lines changed

questions/how-can-you-create-custom-error-objects/en-US.mdx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: How can you create custom error objects?
66

77
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:
88

9-
```js
9+
```js live
1010
class CustomError extends Error {
1111
constructor(message) {
1212
super(message);
@@ -43,7 +43,7 @@ class CustomError extends Error {
4343

4444
You can add custom properties to your custom error class to provide more context about the error.
4545

46-
```js
46+
```js live
4747
class CustomError extends Error {
4848
constructor(message, errorCode) {
4949
super(message);
@@ -65,7 +65,7 @@ try {
6565

6666
You can also add custom methods to your custom error class to handle specific error-related logic.
6767

68-
```js
68+
```js live
6969
class CustomError extends Error {
7070
constructor(message, errorCode) {
7171
super(message);
@@ -89,7 +89,15 @@ try {
8989

9090
You can use the `instanceof` operator to check if an error is an instance of your custom error class.
9191

92-
```js
92+
```js live
93+
class CustomError extends Error {
94+
constructor(message, errorCode) {
95+
super(message);
96+
this.name = 'CustomError';
97+
this.errorCode = errorCode;
98+
}
99+
}
100+
93101
try {
94102
throw new CustomError('This is a custom error message', 404);
95103
} catch (error) {

0 commit comments

Comments
 (0)