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,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.
برای مثال، زمانی که یک `return` درون `try...catch` وجود دارد. بند `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` قرار دهیم، در این موقعیت‌ها اجرا نمی‌شود.
24 changes: 12 additions & 12 deletions 1-js/10-error-handling/1-try-catch/1-finally-or-code-after/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ importance: 5

---

# Finally or just the code?
# بند finally یا فقط کد؟

Compare the two code fragments.
این دو قطعه کد را مقایسه کنید.

1. The first one uses `finally` to execute the code after `try...catch`:
1. کد اول از `finally` برای اجرای کد بعد از `try...catch` استفاده می‌کند:

```js
try {
work work
انجام کارها
} catch (err) {
handle errors
مدیریت ارورها
} finally {
*!*
cleanup the working space
پاک سازی فضاری کاری
*/!*
}
```
2. The second fragment puts the cleaning right after `try...catch`:
2. قطعه دوم پاک سازی را درست بعد از `try...catch` قرار می‌دهد:

```js
try {
work work
انجام کارها
} catch (err) {
handle errors
مدیریت ارورها
}

*!*
cleanup the working space
پاک سازی فضای کاری
*/!*
```

We definitely need the cleanup after the work, 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.
آیا اینجا استفاده از `finally` برتری دارد یا هر دو قطعه کد یکسان هستند؟ اگر برتری وجود داشته باشد، سپس برای زمانی که این برتری مهم است یک مثال بزنید.
Loading