Skip to content

Commit 0f25840

Browse files
暮晨leisurelicht
暮晨
authored andcommitted
EX.Catching the Exceptions
1 parent 4c2027f commit 0f25840

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ So, here we go...
6060
- [> Deleting a list item while iterating/迭代列表时删除元素](#-deleting-a-list-item-while-iterating迭代列表时删除元素)
6161
- [> Loop variables leaking out!/循环变量泄漏!](#-loop-variables-leaking-out循环变量泄漏)
6262
- [> Beware of default mutable arguments!/当心默认的可变参数!](#-beware-of-default-mutable-arguments当心默认的可变参数)
63-
- [> Catching the Exceptions](#-catching-the-exceptions)
63+
- [> Catching the Exceptions/捕获异常](#-catching-the-exceptions捕获异常)
6464
- [> Same operands, different story!](#-same-operands-different-story)
6565
- [> The out of scope variable](#-the-out-of-scope-variable)
6666
- [> Be careful with chained operations](#-be-careful-with-chained-operations)
@@ -1605,18 +1605,18 @@ def some_func(default_arg=[]):
16051605
16061606
---
16071607
1608-
### > Catching the Exceptions
1608+
### > Catching the Exceptions/捕获异常
16091609
16101610
```py
16111611
some_list = [1, 2, 3]
16121612
try:
1613-
# This should raise an ``IndexError``
1613+
# 这里会抛出异常 ``IndexError``
16141614
print(some_list[4])
16151615
except IndexError, ValueError:
16161616
print("Caught!")
16171617
16181618
try:
1619-
# This should raise a ``ValueError``
1619+
# 这里会抛出异常 ``ValueError``
16201620
some_list.remove(4)
16211621
except IndexError, ValueError:
16221622
print("Caught again!")
@@ -1637,13 +1637,13 @@ ValueError: list.remove(x): x not in list
16371637
SyntaxError: invalid syntax
16381638
```
16391639
1640-
#### 💡 Explanation
1640+
#### 💡 说明:
16411641
1642-
* To add multiple Exceptions to the except clause, you need to pass them as parenthesized tuple as the first argument. The second argument is an optional name, which when supplied will bind the Exception instance that has been raised. Example,
1642+
* 如果你想要同时捕获多个不同类型的异常时, 你需要将它们用括号包成一个元组作为第一个参数传递. 第二个参数是可选名称, 如果你提供, 它将与被捕获的异常实例绑定. 例,
16431643
```py
16441644
some_list = [1, 2, 3]
16451645
try:
1646-
# This should raise a ``ValueError``
1646+
# 这里会抛出异常 ``ValueError``
16471647
some_list.remove(4)
16481648
except (IndexError, ValueError), e:
16491649
print("Caught again!")
@@ -1662,7 +1662,7 @@ SyntaxError: invalid syntax
16621662
IndentationError: unindent does not match any outer indentation level
16631663
```
16641664
1665-
* Separating the exception from the variable with a comma is deprecated and does not work in Python 3; the correct way is to use `as`. Example,
1665+
* Python 3 中, 用逗号区分异常与可选名称是无效的; 正确的做法是使用 `as` 关键字. 例,
16661666
```py
16671667
some_list = [1, 2, 3]
16681668
try:

0 commit comments

Comments
 (0)