mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 10:34:08 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
看下面这段代码:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -25,7 +25,7 @@ while True:
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -38,12 +38,12 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> -1
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ValueError Traceback (most recent call last)
|
||||
<ipython-input-1-ceb8cf66641b> in <module>()
|
||||
@@ -59,7 +59,7 @@ ValueError: math domain error
|
||||
|
||||
一旦报错,程序就会停止执行,如果不希望程序停止执行,那么我们可以添加一对 `try & except`:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -81,7 +81,7 @@ while True:
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -97,7 +97,7 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> -1
|
||||
the value must be greater than 0
|
||||
> 0
|
||||
@@ -110,7 +110,7 @@ log10(1.0) = 0.0
|
||||
|
||||
## 捕捉不同的错误类型
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -130,7 +130,7 @@ while True:
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -146,12 +146,12 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> 1
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ZeroDivisionError Traceback (most recent call last)
|
||||
<ipython-input-3-7607f1ae6af9> in <module>()
|
||||
@@ -172,7 +172,7 @@ ZeroDivisionError: float division by zero
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -188,7 +188,7 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> 1
|
||||
invalid value
|
||||
> 0
|
||||
@@ -207,7 +207,7 @@ invalid value
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -223,7 +223,7 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> 1
|
||||
invalid value
|
||||
> -1
|
||||
@@ -238,7 +238,7 @@ invalid value
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -256,7 +256,7 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> 1
|
||||
the value must not be 1
|
||||
> -1
|
||||
@@ -273,7 +273,7 @@ the value must be greater than 0
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -293,7 +293,7 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> 1
|
||||
the value must not be 1
|
||||
> -1
|
||||
@@ -310,12 +310,12 @@ the value must be greater than 0
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
float('a')
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ValueError Traceback (most recent call last)
|
||||
<ipython-input-8-99859da4e72c> in <module>()
|
||||
@@ -328,7 +328,7 @@ ValueError: could not convert string to float: a
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
import math
|
||||
|
||||
while True:
|
||||
@@ -351,7 +351,7 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> 1
|
||||
the value must not be 1
|
||||
> -1
|
||||
@@ -366,19 +366,19 @@ could not convert 'aa' to float
|
||||
|
||||
这里,`exc.message` 显示的内容是异常对应的说明,例如
|
||||
|
||||
```
|
||||
```py
|
||||
ValueError: could not convert string to float: a
|
||||
```
|
||||
|
||||
对应的 `message` 是
|
||||
|
||||
```
|
||||
```py
|
||||
could not convert string to float: a
|
||||
```
|
||||
|
||||
当我们使用 `except Exception` 时,会捕获所有的 `Exception` 和它派生出来的子类,但不是所有的异常都是从 `Exception` 类派生出来的,可能会出现一些不能捕获的情况,因此,更加一般的做法是使用这样的形式:
|
||||
|
||||
```
|
||||
```py
|
||||
try:
|
||||
pass
|
||||
except:
|
||||
@@ -394,7 +394,7 @@ except:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
class CommandError(ValueError):
|
||||
pass
|
||||
|
||||
@@ -404,7 +404,7 @@ class CommandError(ValueError):
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
valid_commands = {'start', 'stop', 'pause'}
|
||||
|
||||
while True:
|
||||
@@ -414,12 +414,12 @@ while True:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
> bad command
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
CommandError Traceback (most recent call last)
|
||||
<ipython-input-11-0e1f81a1136d> in <module>()
|
||||
@@ -434,7 +434,7 @@ CommandError: Invalid commmand: bad command
|
||||
|
||||
我们可以使用 `try/except` 块来捕捉这个异常:
|
||||
|
||||
```
|
||||
```py
|
||||
valid_commands = {'start', 'stop', 'pause'}
|
||||
|
||||
while True:
|
||||
@@ -457,7 +457,7 @@ try/catch 块还有一个可选的关键词 finally。
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
try:
|
||||
print 1
|
||||
finally:
|
||||
@@ -465,7 +465,7 @@ finally:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1
|
||||
finally was called.
|
||||
|
||||
@@ -475,7 +475,7 @@ finally was called.
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
try:
|
||||
print 1 / 0
|
||||
finally:
|
||||
@@ -483,12 +483,12 @@ finally:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
finally was called.
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ZeroDivisionError Traceback (most recent call last)
|
||||
<ipython-input-13-87ecdf8b9265> in <module>()
|
||||
@@ -504,7 +504,7 @@ ZeroDivisionError: integer division or modulo by zero
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
try:
|
||||
print 1 / 0
|
||||
except ZeroDivisionError:
|
||||
@@ -514,7 +514,7 @@ finally:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
divide by 0.
|
||||
finally was called.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user