Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling, "try...catch" #225

Merged
merged 16 commits into from
Dec 7, 2021
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
The difference becomes obvious when we look at the code inside a function.
Різниця стає очевидною, якщо ми подивимося на код всередині функції.

The behavior is different if there's a "jump out" of `try...catch`.
Поведінка відрізнятиметься, якщо код «раптово вийде» з блоку `try...catch`.

For instance, when there's a `return` inside `try...catch`. The `finally` clause works in case of *any* exit from `try...catch`, even via the `return` statement: right after `try...catch` is done, but before the calling code gets the control.
Наприклад, якщо всередині `try...catch` є `return`. Блок `finally` спрацює для "будь-якого" виходу з `try...catch`, навіть за допомогою `return` -- одразу після виходу з блоку `try...catch`, але перед передачею контролю кодові, що викликав цю функцію.

```js run
function f() {
try {
alert('start');
alert('початок');
*!*
return "result";
return "результат";
*/!*
} catch (err) {
/// ...
} finally {
alert('cleanup!');
alert('очищення!');
}
}

f(); // cleanup!
f(); // очищення!
```

...Or when there's a `throw`, like here:
...Або якщо є `throw`:

```js run
function f() {
try {
alert('start');
throw new Error("an error");
alert('початок');
throw new Error("помилка");
} catch (err) {
// ...
if("can't handle the error") {
if("не можу обробити помилку") {
*!*
throw err;
*/!*
}

} finally {
alert('cleanup!')
alert('очищення!')
}
}

f(); // cleanup!
f(); // очищення!
```

It's `finally` that guarantees the cleanup here. If we just put the code at the end of `f`, it wouldn't run in these situations.
`finally` гарантує очищення. Очищення не спрацює, якщо ми просто додамо код в кінці функції `f`.
24 changes: 12 additions & 12 deletions 1-js/10-error-handling/1-try-catch/1-finally-or-code-after/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ importance: 5

---

# Finally or just the code?
# Finally чи просто код?

Compare the two code fragments.
Порівняйте два фрагменти коду.

1. The first one uses `finally` to execute the code after `try...catch`:
1. В першому використовується `finally` для виконання коду після `try...catch`:

```js
try {
work work
виконання коду
} catch (err) {
handle errors
обробка помилок
} finally {
*!*
cleanup the working space
очищення ресурсів
*/!*
}
```
2. The second fragment puts the cleaning right after `try...catch`:
2. В другому коді очищення відбувається одразу після `try...catch`:

```js
try {
work work
виконання коду
} catch (err) {
handle errors
обробка помилок
}

*!*
cleanup the working space
очищення ресурсів
*/!*
```

We definitely need the cleanup after the work, doesn't matter if there was an error or not.
Очищення ресурсів потрібно виконати після роботи не залежно від наявності помилки.

Is there an advantage here in using `finally` or both code fragments are equal? If there is such an advantage, then give an example when it matters.
Чи є якійсь переваги використання `finally`, чи обидва фрагменти коду однакові? Якщо є різниця -- наведіть приклади використання.
Loading