Skip to content

Commit 7ed245d

Browse files
committed
up
1 parent 91279bb commit 7ed245d

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ The extended syntax looks like this:
460460
}
461461
```
462462
463-
Try to run this?
463+
Try running this code:
464464
465465
```js run
466466
try {
@@ -473,16 +473,18 @@ try {
473473
}
474474
```
475475
476-
The code has two variants:
476+
The code has two ways of execution:
477477
478-
1. If say answer "Yes" to error, then `try -> catch -> finally`.
479-
2. If say "No", then `try -> finally`.
478+
1. If you answer "Yes" to "Make an error?", then `try -> catch -> finally`.
479+
2. If you say "No", then `try -> finally`.
480480
481481
The `finally` clause is often used when we start doing something before `try..catch` and want to finalize it in any case of outcome.
482482
483483
For instance, we want to measure time that a Fibonacci numbers function `fib(n)` takes. Naturally, we can start measuring before it runs and finish afterwards. But what is there's an error during the function call? In particular, the implementation of `fib(n)` in the code below returns an error for negative or non-integer numbers.
484484
485-
The `finally` clause is a great place to finish the measurements no matter how the function finishes.
485+
The `finally` clause is a great place to finish the measurements no matter what.
486+
487+
Here `finally` guarantees that the time will be measured correctly in both situations -- in case of a successful execution of `fib` and in case of an error in it:
486488
487489
```js run
488490
let num = +prompt("Enter a positive integer number?", 35)
@@ -513,29 +515,29 @@ alert(result || "error occured");
513515
alert( `execution took ${diff}ms` );
514516
```
515517
516-
Here `finally` guarantees that the time will be measured correctly in both situations -- in case of a successful execution of `fib` and in case of an error in it.
517-
518-
You can check that by running the code with `num=35` -- executes normally, `finally` after `try`, and then with `num=-1`, there will be an immediate error, an the execution will take `0ms`.
518+
You can check by running the code with entering `35` into `prompt` -- it executes normally, `finally` after `try`. And then enter `-1` -- there will be an immediate error, an the execution will take `0ms`. Both measurements are done correctly.
519519
520-
In other words, there may be two ways to exist from a function: either a `return` or `throw`. The `finally` handles them both.
520+
In other words, there may be two ways to exit a function: either a `return` or `throw`. The `finally` clause handles them both.
521521
522522
523-
```smart header="Variables are local to try..catch..finally clauses"
523+
```smart header="Variables are local inside `try..catch..finally`"
524524
Please note that `result` and `diff` variables in the code above are declared *before* `try..catch`.
525525

526-
Otherwise, if `let` were made inside the `{...}` clause, it would only be visible inside of it.
526+
Otherwise, if `let` were made inside the `{...}` block, it would only be visible inside of it.
527527
```
528528
529529
````smart header="`finally` and `return`"
530-
Finally clause works for *any* exit from `try..catch`. That includes the `return` directive.
530+
Finally clause works for *any* exit from `try..catch`. That includes an explicit `return`.
531531
532532
In the example below, there's a `return` in `try`. In this case, `finally` is executed just before the control returns to the outer code.
533533
534534
```js run
535535
function func() {
536536

537537
try {
538+
*!*
538539
return 1;
540+
*/!*
539541

540542
} catch (e) {
541543
/* ... */
@@ -556,15 +558,15 @@ The `try..finally` construct, without `catch` clause, is also useful. We apply i
556558
557559
```js
558560
function func() {
559-
// start doing something that needs completion
561+
// start doing something that needs completion (like measurements)
560562
try {
561563
// ...
562564
} finally {
563565
// complete that thing even if all dies
564566
}
565567
}
566568
```
567-
In the code above, an error inside `try` always falls out, because there's no `catch`. But `finally` triggers before that.
569+
In the code above, an error inside `try` always falls out, because there's no `catch`. But `finally` works before the execution flow jumps outside.
568570
````
569571
570572
## Global catch
@@ -573,9 +575,9 @@ In the code above, an error inside `try` always falls out, because there's no `c
573575
The information from this section is not a part of the core Javascript.
574576
```
575577
576-
Let's imagine we've got a fatal error outside of `try..catch`, and the script died.
578+
Let's imagine we've got a fatal error outside of `try..catch`, and the script died. Like a programming error that no `try..catch` doesn't know how to handle, or something else terrible.
577579
578-
Is there a way to react on it? We may want to log the error, show something to the user (normally he doesn't see the error message) etc.
580+
Is there a way to react on such happening? We may want to log the error, show something to the user (normally he doesn't see the error message) etc.
579581
580582
There is none in the specification, but environments usually provide it, because it's really handy. 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. It will run in case of an uncaught error.
581583
@@ -617,9 +619,9 @@ For instance:
617619
</script>
618620
```
619621
620-
The role of the global handler is usually not to recover the script execution -- that's probably impossible, but to send the error message to developers.
622+
The role of the global handler `window.onerror` is usually not to recover the script execution -- that's probably impossible in case of programming errors, but to send the error message to developers.
621623
622-
There are also web-services that provide error-logging facilities, like <https://errorception.com> or <http://www.muscula.com>. They give a script with custom `window.onerror` function, and once inserted into a page, it reports about all errors it gets to their server. Afterwards developers can browse them and get notifications on email about fresh errors.
624+
There are also web-services that provide error-logging facilities for such cases, like <https://errorception.com> or <http://www.muscula.com>. They give a script with custom `window.onerror` function, and once inserted into a page, it reports about all errors it gets to their server. Afterwards developers can browse them and get notifications on email about fresh errors.
623625
624626
## Summary
625627

0 commit comments

Comments
 (0)