Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Error handling, "try...catch"#202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
khachoangpt wants to merge2 commits intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromkhachoangpt:error-handling
Open
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
The difference becomes obvious when we look at thecodeinside a function.
Sự khác biệt trở nên rõ ràng khi chúng ta xem xétcodebên trong một hàm.

The behavior is different if there's a "jump out" of `try...catch`.
Cách phản ứng sẽ khác nhau nếu có hành vi "nhảy ra" của `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 codegets the control.
Ví dụ, khi có một `return`bên trong`try...catch`.Mệnh đề`finally`hoạt động trên *bất kỳ* lối thoát nào khỏi`try...catch`,ngay cả khi thoát qua câu lệnh`return`: ngay sau khi`try...catch`kết thúc thực thi, nhưng trước khi gọi codegiành quyền kiểm soát.

```js run
function f() {
try {
alert('start');
alert('bắt đâù');
*!*
return "result";
return "kết quả";
*/!*
} catch (err) {
/// ...
} finally {
alert('cleanup!');
alert('dọn dẹp!');
}
}

f(); //cleanup!
f(); //dọn dẹp!
```

...Or when there's a `throw`,like here:
...Hoặc khi có một `throw`,như thế này:

```js run
function f() {
try {
alert('start');
throw new Error("an error");
alert('bắt đầu');
throw new Error("một lỗi");
} catch (err) {
// ...
if("can't handle the error") {
if("không thể xử lý lỗi") {
*!*
throw err;
*/!*
}

} finally {
alert('cleanup!')
alert('dọn dẹp!')
}
}

f(); //cleanup!
f(); //dọn dẹp!
```

It's`finally`that guarantees the cleanup here. If we just put the codeat the end of`f`,it wouldn't run in these situations.
`finally`ở đây đảm bảo việc dọn dẹp. Nếu chúng ta chỉ đặt codeở cuối hàm`f`,thì nó sẽ không chạy trong những tình huống này.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
importance: 5
mức độ quan trọng: 5

---

# Finallyor just the code?
# Finallyhay chỉ dùng code?

Compare the twocodefragments.
So sánh hai đoạncodedươí đây.

1.The first one uses`finally`to execute the codeafter `try...catch`:
1.Đoạn code đầu tiên sử dụng`finally`để thực thi codesau khi `try...catch`:

```js
try {
work work
công việc
} catch (err) {
handle errors
xử lý lỗi
} finally {
*!*
cleanup the working space
dọn dẹp không gian làm việc
*/!*
}
```
2.The second fragment puts the cleaning right after `try...catch`:
2.Đoạn code thứ hai đặt code dọn dẹp ngay sau `try...catch`:

```js
try {
work work
công việc
} catch (err) {
handle errors
xử lý lỗi
}

*!*
cleanup the working space
dọn dẹp không gian làm việc
*/!*
```

We definitely need the cleanup after the work, doesn't matter if there was an error or not.
Chúng tôi chắc chắn cần phải dọn dẹp sau khi làm việc, bất kể có lỗi trong quá trình làm việc hay không.

Is there an advantage here in using `finally`or bothcodefragments are equal? If there is such an advantage, then give an example when it matters.
Có lợi thế nào khi sử dụng `finally`ở đây không hay cả hai đoạncodelà tương đương nhau? Nếu có một lợi thế nào đó ở đây, xin vui lòng cho một ví dụ nếu cần thiết.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp