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: 1-js/10-error-handling/1-try-catch/article.md
+12-52Lines changed: 12 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -353,73 +353,33 @@ try {
353
353
354
354
Of course, everything's possible! Programmers do make mistakes. Even in open-source utilities used by millions for decades -- suddenly a bug may be discovered that leads to terrible hacks.
355
355
356
-
In our case, `try..catch` is meant to catch "incorrect data" errors. But by its nature, `catch` gets *all* errors from `try`. Here it gets an unexpected error, but still shows the same `"JSONError"` message. That's wrong and also makes the code more difficult to debug.
356
+
In our case, `try..catch` is placed to catch "incorrect data" errors. But by its nature, `catch` gets *all* errors from `try`. Here it gets an unexpected error, but still shows the same `"JSONError"` message. That's wrong and also makes the code more difficult to debug.
357
357
358
-
Fortunately, there are multiple ways to determine the type of error. Common methods are shown below.
358
+
To avoid such problems, we can employ the "rethrowing" technique. The rule is simple:
359
359
360
-
Using its `name` property:
361
-
362
-
```js run
363
-
try {
364
-
user = { /*...*/ };
365
-
} catch(e) {
366
-
*!*
367
-
alert(e.name); // "ReferenceError" for accessing an undefined variable
368
-
*/!*
369
-
}
370
-
```
360
+
**Catch should only process errors that it knows and "rethrow" all others.**
371
361
372
-
Using its `constructor.name` property (read-only):
362
+
The "rethrowing" technique can be explained in more detail as:
373
363
374
-
```js run
375
-
try {
376
-
user = { /*...*/ };
377
-
} catch(e) {
378
-
*!*
379
-
alert(e.constructor.name); // "ReferenceError" for accessing an undefined variable
380
-
*/!*
381
-
}
382
-
```
364
+
1. Catch gets all errors.
365
+
2. In the `catch(err) {...}` block we analyze the error object `err`.
366
+
2. If we don't know how to handle it, we do `throw err`.
383
367
384
-
Comparing its `constructor` property to the specific error type:
368
+
Usually, we can check the error type using the `instanceof` operator:
385
369
386
370
```js run
387
371
try {
388
372
user = { /*...*/ };
389
-
} catch(e) {
373
+
} catch(err) {
390
374
*!*
391
-
if (e.constructor === RefferenceError) {
392
-
alert('e is a ReferenceError'); // "ReferenceError" for accessing an undefined variable
393
-
}
375
+
if (err instanceof ReferenceError) {
394
376
*/!*
395
-
}
396
-
```
397
-
398
-
Comparing its class type to another error type using the `instanceof` operator:
399
-
400
-
```js run
401
-
try {
402
-
user = { /*...*/ };
403
-
} catch(e) {
404
-
*!*
405
-
if (e instanceof RefferenceError) {
406
-
alert('e is an instance of ReferenceError'); // "ReferenceError" for accessing an undefined variable
377
+
alert('ReferenceError'); // "ReferenceError" for accessing an undefined variable
407
378
}
408
-
*/!*
409
379
}
410
380
```
411
381
412
-
MDN suggests using the `constructor` property or `instanceof` operator in its [error examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Handling_a_specific_error).
413
-
414
-
The rule is simple:
415
-
416
-
**Catch should only process errors that it knows and "rethrow" all others.**
417
-
418
-
The "rethrowing" technique can be explained in more detail as:
419
-
420
-
1. Catch gets all errors.
421
-
2. In the `catch(err) {...}` block we analyze the error object `err`.
422
-
2. If we don't know how to handle it, we do `throw err`.
382
+
We can also get the error class name from `err.name` property. All native errors have it. Another option is to read `err.constructor.name`.
423
383
424
384
In the code below, we use rethrowing so that `catch` only handles `SyntaxError`:
0 commit comments