Skip to content
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.
A diferença fica óbvia quando olhamos o código dentro de uma função.

The behavior is different if there's a "jump out" of `try...catch`.
O comportamento é diferente quando há um "salto para fora" do `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.
Por exemplo, quando há um `return` dentro do `try...catch`. A cláusula `finally` funciona em caso de *qualquer* saída do `try...catch`, mesmo através do comando `return`: logo depois que o `try...catch` termina, mas antes do código que fez a chamada receber o controle.

```js run
function f() {
try {
alert('start');
alert('início');
*!*
return "result";
*/!*
} catch (err) {
/// ...
} finally {
alert('cleanup!');
alert('limpeza!');
}
}

f(); // cleanup!
f(); // limpeza!
```

...Or when there's a `throw`, like here:
...Ou quando há um `throw`, como aqui:

```js run
function f() {
try {
alert('start');
alert('início');
throw new Error("an error");
} catch (err) {
// ...
if("can't handle the error") {
if("não sabe tratar o erro") {
*!*
throw err;
*/!*
}

} finally {
alert('cleanup!')
alert('limpeza!')
}
}

f(); // cleanup!
f(); // limpeza!
```

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.
É o `finally` que garante a limpeza aqui. Se apenas colocássemos o código no fim de `f`, ele não rodaria nessas situações.
24 changes: 12 additions & 12 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?
# Finally, ou só o código?

Compare the two code fragments.
Compare os dois trechos de código.

1. The first one uses `finally` to execute the code after `try...catch`:
1. O primeiro usa `finally` para executar o código depois do `try...catch`:

```js
try {
work work
faz o trabalho
} catch (err) {
handle errors
trata os erros
} finally {
*!*
cleanup the working space
limpa o espaço de trabalho
*/!*
}
```
2. The second fragment puts the cleaning right after `try...catch`:
2. O segundo trecho coloca a limpeza logo depois do `try...catch`:

```js
try {
work work
faz o trabalho
} catch (err) {
handle errors
trata os erros
}

*!*
cleanup the working space
limpa o espaço de trabalho
*/!*
```

We definitely need the cleanup after the work, doesn't matter if there was an error or not.
Com certeza precisamos da limpeza depois do trabalho, não importa se houve um erro ou não.

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.
Existe alguma vantagem em usar o `finally` aqui, ou os dois trechos de código são equivalentes? Se houver essa vantagem, dê um exemplo de quando isso importa.
Loading