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
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -201,7 +201,7 @@ You can find more detailed information about JSON in the <info:json> chapter.
201
201
202
202
**If `json` is malformed, `JSON.parse` generates an error, so the script "dies".**
203
203
204
-
Should we be satisfied with that? Of course, not!
204
+
Should we be satisfied with that? Of course not!
205
205
206
206
This way, if something's wrong with the data, the visitor will never know that (unless they open the developer console). And people really don't like when something "just dies" without any error message.
207
207
@@ -334,9 +334,9 @@ Now `catch` became a single place for all error handling: both for `JSON.parse`
334
334
335
335
## Rethrowing
336
336
337
-
In the example above we use `try..catch` to handle incorrect data. But is it possible that *another unexpected error* occurs within the `try {...}` block? Like a programming error (variable is not defined) or something else, not just that"incorrect data" thing.
337
+
In the example above we use `try..catch` to handle incorrect data. But is it possible that *another unexpected error* occurs within the `try {...}` block? Like a programming error (variable is not defined) or something else, not just this"incorrect data" thing.
338
338
339
-
Like this:
339
+
For example:
340
340
341
341
```js run
342
342
let json = '{ "age": 30 }'; // incomplete data
@@ -374,8 +374,8 @@ The rule is simple:
374
374
The "rethrowing" technique can be explained in more detail as:
375
375
376
376
1. Catch gets all errors.
377
-
2. In `catch(err) {...}` block we analyze the error object `err`.
378
-
2. If we don't know how to handle it, then we do `throw err`.
377
+
2. In the `catch(err) {...}` block we analyze the error object `err`.
378
+
2. If we don't know how to handle it, we do `throw err`.
379
379
380
380
In the code below, we use rethrowing so that `catch` only handles `SyntaxError`:
381
381
@@ -582,11 +582,11 @@ In the code above, an error inside `try` always falls out, because there's no `c
582
582
The information from this section is not a part of the core JavaScript.
583
583
```
584
584
585
-
Let's imagine we've got a fatal error outside of `try..catch`, and the script died. Like a programming error or something else terrible.
585
+
Let's imagine we've got a fatal error outside of `try..catch`, and the script died. Like a programming error or some other terrible thing.
586
586
587
-
Is there a way to react on such occurrences? We may want to log the error, show something to the user (normally they don't see error messages) etc.
587
+
Is there a way to react on such occurrences? We may want to log the error, show something to the user (normally they don't see error messages), etc.
588
588
589
-
There is none in the specification, but environments usually provide it, because it's really useful. For instance, Node.js has [`process.on("uncaughtException")`](https://nodejs.org/api/process.html#process_event_uncaughtexception) for that. And in the browser we can assign a function to special [window.onerror](mdn:api/GlobalEventHandlers/onerror) property, that will run in case of an uncaught error.
589
+
There is none in the specification, but environments usually provide it, because it's really useful. For instance, Node.js has [`process.on("uncaughtException")`](https://nodejs.org/api/process.html#process_event_uncaughtexception) for that. And in the browser we can assign a function to the special [window.onerror](mdn:api/GlobalEventHandlers/onerror) property, that will run in case of an uncaught error.
0 commit comments