Skip to content

Commit fd8848b

Browse files
authored
Merge pull request #194 from mahdiHash/master
Error handling, "try...catch"
2 parents f5b8bce + 1449c45 commit fd8848b

File tree

3 files changed

+226
-226
lines changed

3 files changed

+226
-226
lines changed
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
The difference becomes obvious when we look at the code inside a function.
1+
تفاوت زمانی آشکار می‌شود که ما درون یک تابع به کد نگاه کنیم.
22

3-
The behavior is different if there's a "jump out" of `try...catch`.
3+
اگر یک «پرش به بیرون» از `try...catch` وجود داشته باشد، رفتار متفاوت است.
44

5-
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.
5+
برای مثال، زمانی که یک `return` درون `try...catch` وجود دارد. بند `finally` در صورت *هر گونه* خارج شدن از `try...catch` کار می‌کند حتی با دستور `return`: درست بعد تمام شدن `try...catch` اما قبل از اینکه کد فراخوانی شده کنترل را به دست بگیرد.
66

77
```js run
88
function f() {
99
try {
10-
alert('start');
10+
alert('شروع');
1111
*!*
12-
return "result";
12+
return "نتیجه";
1313
*/!*
1414
} catch (err) {
1515
/// ...
1616
} finally {
17-
alert('cleanup!');
17+
alert('پاک سازی!');
1818
}
1919
}
2020

21-
f(); // cleanup!
21+
f(); // !پاک سازی
2222
```
2323

24-
...Or when there's a `throw`, like here:
24+
...یا زمانی که یک `throw` وجود داشته باشد، مثل اینجا:
2525

2626
```js run
2727
function f() {
2828
try {
29-
alert('start');
30-
throw new Error("an error");
29+
alert('شروع');
30+
throw new Error("یک ارور");
3131
} catch (err) {
3232
// ...
33-
if("can't handle the error") {
33+
if("نمی‌توانی ارور را مدیریت کنی") {
3434
*!*
3535
throw err;
3636
*/!*
3737
}
3838

3939
} finally {
40-
alert('cleanup!')
40+
alert('پاک سازی!')
4141
}
4242
}
4343

44-
f(); // cleanup!
44+
f(); // !پاک سازی
4545
```
4646

47-
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.
47+
این `finally` است که در اینجا پاک سازی را تضمین می‌کند. اگر ما فقط کد را در انتهای `f` قرار دهیم، در این موقعیت‌ها اجرا نمی‌شود.

1-js/10-error-handling/1-try-catch/1-finally-or-code-after/task.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@ importance: 5
22

33
---
44

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

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

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

1111
```js
1212
try {
13-
work work
13+
انجام کارها
1414
} catch (err) {
15-
handle errors
15+
مدیریت ارورها
1616
} finally {
1717
*!*
18-
cleanup the working space
18+
پاک سازی فضاری کاری
1919
*/!*
2020
}
2121
```
22-
2. The second fragment puts the cleaning right after `try...catch`:
22+
2. قطعه دوم پاک سازی را درست بعد از `try...catch` قرار می‌دهد:
2323

2424
```js
2525
try {
26-
work work
26+
انجام کارها
2727
} catch (err) {
28-
handle errors
28+
مدیریت ارورها
2929
}
3030
3131
*!*
32-
cleanup the working space
32+
پاک سازی فضای کاری
3333
*/!*
3434
```
3535

36-
We definitely need the cleanup after the work, doesn't matter if there was an error or not.
36+
ما قطعا به پاک سازی بعد از کار نیاز داریم، مهم نیست که ارور وجود داشته باشد یا خیر.
3737

38-
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.
38+
آیا اینجا استفاده از `finally` برتری دارد یا هر دو قطعه کد یکسان هستند؟ اگر برتری وجود داشته باشد، سپس برای زمانی که این برتری مهم است یک مثال بزنید.

0 commit comments

Comments
 (0)