Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: javascript-tutorial/zh.javascript.info
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: lance10030/javascript-tutorial-en
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Jun 7, 2018

  1. Copy the full SHA
    22ff628 View commit details
Showing with 5 additions and 11 deletions.
  1. +5 −11 1-js/08-error-handling/1-try-catch/1-finally-or-code-after/solution.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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`.
如果能够跳出 `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.
例如,当 try..catch 里有 return 的时候,finally 代码块会在退出 try..catch 后继续执行,即使是通过 return 语句退出的。它会在再后面的代码执行之前,在 try..catch 执行完之后立即执行。

```js run
function f() {
try {
alert('start');
@@ -19,11 +18,8 @@ function f() {
}

f(); // cleanup!
```
...或者当有 throw 的时候,如下:

...Or when there's a `throw`, like here:

```js run
function f() {
try {
alert('start');
@@ -42,6 +38,4 @@ function f() {
}

f(); // cleanup!
```

It's `finally` that guarantees the cleanup here. If we just put the code at the end of `f`, it wouldn't run.
它保证了 cleanup 的显示,但是如果把这行代码放在 函数 f 之后,那么,它不会执行。