en.javascript.info/1-js/08-error-handling/1-try-catch/1-finally-or-code-after/task.md
Ilya Kantor 9ad9063d00 up
2016-11-28 21:35:42 +03:00

38 lines
767 B
Markdown

importance: 5
---
# Finally or just the code?
Compare the two code fragments.
1. The first one uses `finally` to execute the code after `try..catch`:
```js
try {
work work
} catch (e) {
handle errors
} finally {
*!*
cleanup the working space
*/!*
}
```
2. The second fragment puts the cleaning right after `try..catch`:
```js
try {
work work
} catch (e) {
handle errors
}
*!*
cleanup the working space
*/!*
```
We definitely need the cleanup after the work has started, doesn't matter if there was an error or not.
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.