Skip to content

Error handling, "try..catch" #279

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 11 commits into from
Jul 13, 2020
Merged
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.
La diferencia se hace evidente cuando miramos el código dentro de una función.

The behavior is different if there's a "jump out" of `try..catch`.
El comportamiento es diferente si hay un "salto fuera" de `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 ejemplo, cuando hay un `return` en el interior de `try..catch`. La cláusula `finally` funciona en el caso de *cualquier* salida de `try..catch`, incluso a través de la declaración `return`: justo después de que `try..catch` haya terminado, pero antes de que el código de llamada obtenga el control.

```js run
function f() {
try {
alert('start');
alert('inicio');
*!*
return "result";
return "resultado";
*/!*
} catch (e) {
/// ...
} finally {
alert('cleanup!');
alert('limpieza!');
}
}

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

...Or when there's a `throw`, like here:
... O cuando hay un `throw` (lanzamiento de excepción), como aquí:

```js run
function f() {
try {
alert('start');
throw new Error("an error");
alert('inicio');
throw new Error("un error");
} catch (e) {
// ...
if("can't handle the error") {
if("no puede manejar el error") {
*!*
throw e;
*/!*
}

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

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

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.
Es "finally" el que garantiza la limpieza aquí. Si acabamos de poner el código al final de `f`, no se ejecutará en estas situaciones.
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 o solo el código?

Compare the two code fragments.
Compara los dos fragmentos de código.

1. The first one uses `finally` to execute the code after `try..catch`:
1. El primero usa `finally` para ejecutar el código después de `try..catch`:

```js
try {
work work
trabajo trabajo
} catch (e) {
handle errors
maneja errores
} finally {
*!*
cleanup the working space
limpiar el espacio de trabajo
*/!*
}
```
2. The second fragment puts the cleaning right after `try..catch`:
2. El segundo fragmento coloca la limpieza justo después de `try..catch`:

```js
try {
work work
trabajo trabajo
} catch (e) {
handle errors
manejo de errores
}

*!*
cleanup the working space
limpiar el espacio de trabajo
*/!*
```

We definitely need the cleanup after the work, doesn't matter if there was an error or not.
Definitivamente necesitamos la limpieza después del trabajo, no importa si hubo un error o no.

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.
¿Hay alguna ventaja aquí en usar `finally` o ambos fragmentos de código son iguales? Si existe tal ventaja, entonces da un ejemplo cuando sea importante.
Loading