-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error handling, "try...catch" (#225)
- Loading branch information
Showing
5 changed files
with
240 additions
and
240 deletions.
There are no files selected for viewing
28 changes: 14 additions & 14 deletions
28
1-js/10-error-handling/1-try-catch/1-finally-or-code-after/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
Різниця стає очевидною, якщо ми подивимося на код всередині функції. | ||
|
||
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'); | ||
alert('початок'); | ||
*!* | ||
return "result"; | ||
return "результат"; | ||
*/!* | ||
} catch (err) { | ||
/// ... | ||
} finally { | ||
alert('cleanup!'); | ||
alert('очищення!'); | ||
} | ||
} | ||
|
||
f(); // cleanup! | ||
f(); // очищення! | ||
``` | ||
|
||
...Or when there's a `throw`, like here: | ||
...Або якщо є `throw`: | ||
|
||
```js run | ||
function f() { | ||
try { | ||
alert('start'); | ||
throw new Error("an error"); | ||
alert('початок'); | ||
throw new Error("помилка"); | ||
} catch (err) { | ||
// ... | ||
if("can't handle the error") { | ||
if("не можу обробити помилку") { | ||
*!* | ||
throw err; | ||
*/!* | ||
} | ||
|
||
} finally { | ||
alert('cleanup!') | ||
alert('очищення!') | ||
} | ||
} | ||
|
||
f(); // cleanup! | ||
f(); // очищення! | ||
``` | ||
|
||
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. | ||
`finally` гарантує очищення. Очищення не спрацює, якщо ми просто додамо код в кінці функції `f`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.