minor fixes
This commit is contained in:
parent
7ef7c5ebf7
commit
da1d600ecd
1 changed files with 18 additions and 58 deletions
|
@ -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.
|
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.
|
To avoid such problems, we can employ the "rethrowing" technique. The rule is simple:
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
**Catch should only process errors that it knows and "rethrow" all others.**
|
**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. 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`.
|
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`:
|
In the code below, we use rethrowing so that `catch` only handles `SyntaxError`:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue