Skip to content

Commit bd8ad7a

Browse files
authored
Merge pull request #279 from cortizg/es.javascript.info.1-10-01-tc
Error handling, "try..catch"
2 parents b46487d + 4f90943 commit bd8ad7a

File tree

4 files changed

+236
-236
lines changed

4 files changed

+236
-236
lines changed
Lines changed: 14 additions & 14 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+
La diferencia se hace evidente cuando miramos el código dentro de una función.
22

3-
The behavior is different if there's a "jump out" of `try..catch`.
3+
El comportamiento es diferente si hay un "salto fuera" de `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+
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.
66

77
```js run
88
function f() {
99
try {
10-
alert('start');
10+
alert('inicio');
1111
*!*
12-
return "result";
12+
return "resultado";
1313
*/!*
1414
} catch (e) {
1515
/// ...
1616
} finally {
17-
alert('cleanup!');
17+
alert('limpieza!');
1818
}
1919
}
2020

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

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

2626
```js run
2727
function f() {
2828
try {
29-
alert('start');
30-
throw new Error("an error");
29+
alert('inicio');
30+
throw new Error("un error");
3131
} catch (e) {
3232
// ...
33-
if("can't handle the error") {
33+
if("no puede manejar el error") {
3434
*!*
3535
throw e;
3636
*/!*
3737
}
3838

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

44-
f(); // cleanup!
44+
f(); // limpieza!
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+
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.

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

Lines changed: 12 additions & 12 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+
# Finally o solo el código?
66

7-
Compare the two code fragments.
7+
Compara los dos fragmentos de código.
88

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

1111
```js
1212
try {
13-
work work
13+
trabajo trabajo
1414
} catch (e) {
15-
handle errors
15+
maneja errores
1616
} finally {
1717
*!*
18-
cleanup the working space
18+
limpiar el espacio de trabajo
1919
*/!*
2020
}
2121
```
22-
2. The second fragment puts the cleaning right after `try..catch`:
22+
2. El segundo fragmento coloca la limpieza justo después de `try..catch`:
2323

2424
```js
2525
try {
26-
work work
26+
trabajo trabajo
2727
} catch (e) {
28-
handle errors
28+
manejo de errores
2929
}
3030
3131
*!*
32-
cleanup the working space
32+
limpiar el espacio de trabajo
3333
*/!*
3434
```
3535

36-
We definitely need the cleanup after the work, doesn't matter if there was an error or not.
36+
Definitivamente necesitamos la limpieza después del trabajo, no importa si hubo un error o no.
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+
¿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.

0 commit comments

Comments
 (0)