Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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.
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") {
*!*
throw err;
throw chyba;
*/!*
}

} 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.
28 changes: 14 additions & 14 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?
# 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 second fragment puts the cleaning right after `try...catch`:
2. Druhý fragment umisť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