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"#190

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
otmon76 merged 2 commits intojavascript-tutorial:masterfromotmon76:1.10.1
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
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.
Rozdíl uvidíme, když se podíváme na kód uvnitř funkce.

The behavior is different if there's a "jump out" of `try...catch`.
Chování se liší, pokud v něm je „vyskočení“ z `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.
Například když uvnitř `try...catch` je `return`. Klauzule `finally`se spustí při *jakémkoli* opuštění`try...catch`,dokonce i příkazem `return`: ihned po ukončení`try...catch`, ale ještě předtím, než řízení převezme volající kód.

```js run
function f() {
try {
alert('start');
alert('začátek');
*!*
return "result";
return "výsledek";
*/!*
} catch (err) {
} catch (chyba) {
/// ...
} finally {
alert('cleanup!');
alert('úklid!');
}
}

f(); //cleanup!
f(); //úklid!
```

...Or when there's a `throw`,like here:
...Nebo když je tam `throw`,například:

```js run
function f() {
try {
alert('start');
throw new Error("an error");
} catch (err) {
alert('začátek');
throw new Error("chyba");
} catch (chyba) {
// ...
if("can't handle the error") {
if("nemůžeme ošetřit chybu") {
*!*
throwerr;
throwchyba;
*/!*
}

} finally {
alert('cleanup!')
alert('úklid!')
}
}

f(); //cleanup!
f(); //úklid!
```

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.
Úklid nám zde zajistí právě `finally`. Kdybychom umístili kód na konec funkce`f`,v těchto situacích by se nespustil.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,37 +2,37 @@ importance: 5

---

#Finally or just the code?
#Klauzule finally nebo jen kód?

Compare the two code fragments.
Porovnejte si tyto dva fragmenty kódu.

1.The first one uses`finally`to execute the code after `try...catch`:
1.První používá`finally`ke spuštění kódu po `try...catch`:

```js
try {
work work
} catch (err) {
handle errors
pracuj pracuj
} catch (chyba) {
ošetření chyb
} finally {
*!*
cleanup the working space
úklid pracovního prostoru
*/!*
}
```
2.The secondfragmentputs the cleaning right after `try...catch`:
2.Druhýfragmentumisťuje úklid hned za `try...catch`:

```js
try {
work work
} catch (err) {
handle errors
pracuj pracuj
} catch (chyba) {
ošetření chyb
}

*!*
cleanup the working space
úklid pracovního prostoru
*/!*
```

We definitely need the cleanup after the work, doesn't matter if there was an error or not.
Úklid po práci potřebujeme provést v každém případě, ať už nastala chyba nebo ne.

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.
Je zde nějaká výhoda v použití`finally`, nebo jsou oba fragmenty kódu rovnocenné? Pokud je nějaká výhoda, vymyslete příklad, v němž se projeví.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp