Skip to content

Commit da1d600

Browse files
committed
minor fixes
1 parent 7ef7c5e commit da1d600

File tree

1 file changed

+12
-52
lines changed

1 file changed

+12
-52
lines changed

1-js/10-error-handling/1-try-catch/article.md

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -353,73 +353,33 @@ try {
353353
354354
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.
355355
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 `"JSON Error"` 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 `"JSON Error"` message. That's wrong and also makes the code more difficult to debug.
357357
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:
359359
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.**
371361
372-
Using its `constructor.name` property (read-only):
362+
The "rethrowing" technique can be explained in more detail as:
373363
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`.
383367
384-
Comparing its `constructor` property to the specific error type:
368+
Usually, we can check the error type using the `instanceof` operator:
385369
386370
```js run
387371
try {
388372
user = { /*...*/ };
389-
} catch(e) {
373+
} catch(err) {
390374
*!*
391-
if (e.constructor === RefferenceError) {
392-
alert('e is a ReferenceError'); // "ReferenceError" for accessing an undefined variable
393-
}
375+
if (err instanceof ReferenceError) {
394376
*/!*
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
407378
}
408-
*/!*
409379
}
410380
```
411381
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`.
423383
424384
In the code below, we use rethrowing so that `catch` only handles `SyntaxError`:
425385

0 commit comments

Comments
 (0)