Skip to content

Commit 43b6afc

Browse files
authored
Merge pull request #190 from otmon76/1.10.1
Error handling, "try...catch"
2 parents c874622 + 3c18496 commit 43b6afc

File tree

4 files changed

+303
-304
lines changed

4 files changed

+303
-304
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
The difference becomes obvious when we look at the code inside a function.
1+
Rozdíl uvidíme, když se podíváme na kód uvnitř funkce.
22

3-
The behavior is different if there's a "jump out" of `try...catch`.
3+
Chování se liší, pokud v něm je „vyskočení“ z `try...catch`.
44

5-
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.
5+
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.
66

77
```js run
88
function f() {
99
try {
10-
alert('start');
10+
alert('začátek');
1111
*!*
12-
return "result";
12+
return "výsledek";
1313
*/!*
14-
} catch (err) {
14+
} catch (chyba) {
1515
/// ...
1616
} finally {
17-
alert('cleanup!');
17+
alert('úklid!');
1818
}
1919
}
2020

21-
f(); // cleanup!
21+
f(); // úklid!
2222
```
2323

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

2626
```js run
2727
function f() {
2828
try {
29-
alert('start');
30-
throw new Error("an error");
31-
} catch (err) {
29+
alert('začátek');
30+
throw new Error("chyba");
31+
} catch (chyba) {
3232
// ...
33-
if("can't handle the error") {
33+
if("nemůžeme ošetřit chybu") {
3434
*!*
35-
throw err;
35+
throw chyba;
3636
*/!*
3737
}
3838

3939
} finally {
40-
alert('cleanup!')
40+
alert('úklid!')
4141
}
4242
}
4343

44-
f(); // cleanup!
44+
f(); // úklid!
4545
```
4646

47-
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.
47+
Ú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.

1-js/10-error-handling/1-try-catch/1-finally-or-code-after/task.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@ importance: 5
22

33
---
44

5-
# Finally or just the code?
5+
# Klauzule finally nebo jen kód?
66

7-
Compare the two code fragments.
7+
Porovnejte si tyto dva fragmenty kódu.
88

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

1111
```js
1212
try {
13-
work work
14-
} catch (err) {
15-
handle errors
13+
pracuj pracuj
14+
} catch (chyba) {
15+
ošetření chyb
1616
} finally {
1717
*!*
18-
cleanup the working space
18+
úklid pracovního prostoru
1919
*/!*
2020
}
2121
```
22-
2. The second fragment puts the cleaning right after `try...catch`:
22+
2. Druhý fragment umisťuje úklid hned za `try...catch`:
2323

2424
```js
2525
try {
26-
work work
27-
} catch (err) {
28-
handle errors
26+
pracuj pracuj
27+
} catch (chyba) {
28+
ošetření chyb
2929
}
3030
3131
*!*
32-
cleanup the working space
32+
úklid pracovního prostoru
3333
*/!*
3434
```
3535

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

38-
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.
38+
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í.

0 commit comments

Comments
 (0)