Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tarasyyyk merged 16 commits intojavascript-tutorial:masterfromPurusah:master
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff 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`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,37 +2,37 @@ importance: 5

---

# Finallyor 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

[8]ページ先頭

©2009-2025 Movatter.jp