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

Closed
wants to merge 8 commits into from
Closed
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.
Jeśli umieścimy pierwszy fragment w środku funkcji, szybko zauważymy różnicę.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W tłumaczeniu jest trochę zmienione znaczenie, "szybko", to nie to samo co "becomes obvious"


The behavior is different if there's a "jump out" of `try..catch`.
Występuje ona w momencie wyjścia z instrukcji `try...catch`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tutaj również zmienione jest znaczenie. Nie mówię, że powinno być 100% słowo w słowo, ale znaczeniowo trochę odbiega od oryginału


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.
Spójrzmy na przykład użycia `return` w środku `try...catch`. Trzeci blok `finally` wykona się dla *każdego* możliwego wyjścia z dwóch dostępnych bloków instrukcji `try...catch...finally`. Nawet jeśli użyjemy instrukcji `return`.

```js run
function f() {
try {
alert('start');
alert('spróbuj wyświetlić okno dialogowe');
*!*
return "result";
return "opuść blok try";
*/!*
} catch (e) {
/// ...
} finally {
alert('cleanup!');
alert('sfinalizuj instrukcję');
}
}

f(); // cleanup!
f(); // sfinalizuj instrukcję
```

...Or when there's a `throw`, like here:
lub kiedy używamy operatora `throw`, jak w przykładzie poniżej:

```js run
function f() {
try {
alert('start');
throw new Error("an error");
alert('spróbuj wyświetlić okno dialogowe');
throw new Error("błąd!");
} catch (e) {
// ...
if("can't handle the error") {
if("nie interesują mnie błędy") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

*!*
throw e;
*/!*
}

} finally {
alert('cleanup!')
alert('sfinalizuj instrukcję')
}
}

f(); // cleanup!
f(); // sfinalizuj instrukcję
```

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.
To blok `finally` zagwarantuje sfinalizowanie instrukcji. Gdy użyjemy zawartości trzeciego bloku na zewnątrz, tuż przed końcem funkcji `f`, nie zostanie to wykonane w przedstawionych scenariuszach.
25 changes: 12 additions & 13 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,36 @@ importance: 5

---

# Finally or just the code?
# Czy blok finally jest rzeczywiście potrzebny?

Compare the two code fragments.
Porównaj obydwa fragmenty programu:

1. The first one uses `finally` to execute the code after `try..catch`:
1. Pierwszy fragment zawiera blok `finally`, naturalnie wykona się jako ostatni ze wszystkich bloków instrukcji:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odbiega od oryginału


```js
try {
work work
// spróbuj wykonać instrukcje
} catch (e) {
handle errors
// obsłuż błąd
} finally {
*!*
cleanup the working space
// sfinalizuj instrukcję
*/!*
}
```
2. The second fragment puts the cleaning right after `try..catch`:
2. Drugi fragment nie zawiera bloku `finally`. Instrukcja `try...catch` zostaje sfinalizowana na zewnątrz, zaraz po jej całkowitym wykonaniu.

```js
try {
work work
// spróbuj wykonać instrukcje
} catch (e) {
handle errors
// obsłuż błędy
}

*!*
cleanup the working space
// sfinalizuj instrukcję
*/!*
```
Bez względu na to czy pojawi się błąd czy też nie, chcemy sfinalizować instrukcję.

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.
Czy istnieje jakaś różnica między powyższymi fragmentami kodu, wynikająca z używania bloku `finally`? Jeśli tak to przedstaw przykład kiedy ma to znaczenie. A może oba fragmenty są sobie równe?
Loading