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,8 +1,8 @@
The difference becomes obvious when we look at the code inside a function.

The behavior is different if there's a "jump out" of `try..catch`.
The behavior is different if there's a "jump out" of `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.
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.

```js run
function f() {
Expand All @@ -11,7 +11,7 @@ function f() {
*!*
return "result";
*/!*
} catch (e) {
} catch (err) {
/// ...
} finally {
alert('cleanup!');
Expand All @@ -28,11 +28,11 @@ function f() {
try {
alert('start');
throw new Error("an error");
} catch (e) {
} catch (err) {
// ...
if("can't handle the error") {
*!*
throw e;
throw err;
*/!*
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ importance: 5

Compare the two code fragments.

1. The first one uses `finally` to execute the code after `try..catch`:
1. The first one uses `finally` to execute the code after `try...catch`:

```js
try {
work work
} catch (e) {
} catch (err) {
handle errors
} finally {
*!*
cleanup the working space
*/!*
}
```
2. The second fragment puts the cleaning right after `try..catch`:
2. The second fragment puts the cleaning right after `try...catch`:

```js
try {
work work
} catch (e) {
} catch (err) {
handle errors
}

Expand Down
Loading