From da1d600ecdefdceaa6d69916fac2c40df86cb830 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 17 May 2020 23:59:55 +0300 Subject: [PATCH] minor fixes --- 1-js/10-error-handling/1-try-catch/article.md | 76 +++++-------------- 1 file changed, 18 insertions(+), 58 deletions(-) diff --git a/1-js/10-error-handling/1-try-catch/article.md b/1-js/10-error-handling/1-try-catch/article.md index 094b62a2..7d41b71b 100644 --- a/1-js/10-error-handling/1-try-catch/article.md +++ b/1-js/10-error-handling/1-try-catch/article.md @@ -353,65 +353,9 @@ try { 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. -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. +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. -Fortunately, there are multiple ways to determine the type of error. Common methods are shown below. - -Using its `name` property: - -```js run -try { - user = { /*...*/ }; -} catch(e) { -*!* - alert(e.name); // "ReferenceError" for accessing an undefined variable -*/!* -} -``` - -Using its `constructor.name` property (read-only): - -```js run -try { - user = { /*...*/ }; -} catch(e) { -*!* - alert(e.constructor.name); // "ReferenceError" for accessing an undefined variable -*/!* -} -``` - -Comparing its `constructor` property to the specific error type: - -```js run -try { - user = { /*...*/ }; -} catch(e) { -*!* - if (e.constructor === RefferenceError) { - alert('e is a ReferenceError'); // "ReferenceError" for accessing an undefined variable - } -*/!* -} -``` - -Comparing its class type to another error type using the `instanceof` operator: - -```js run -try { - user = { /*...*/ }; -} catch(e) { -*!* - if (e instanceof RefferenceError) { - alert('e is an instance of ReferenceError'); // "ReferenceError" for accessing an undefined variable - } -*/!* -} -``` - -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). - -The rule is simple: +To avoid such problems, we can employ the "rethrowing" technique. The rule is simple: **Catch should only process errors that it knows and "rethrow" all others.** @@ -421,6 +365,22 @@ The "rethrowing" technique can be explained in more detail as: 2. In the `catch(err) {...}` block we analyze the error object `err`. 2. If we don't know how to handle it, we do `throw err`. +Usually, we can check the error type using the `instanceof` operator: + +```js run +try { + user = { /*...*/ }; +} catch(err) { +*!* + if (err instanceof ReferenceError) { +*/!* + alert('ReferenceError'); // "ReferenceError" for accessing an undefined variable + } +} +``` + +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`. + In the code below, we use rethrowing so that `catch` only handles `SyntaxError`: ```js run