本文主要是介绍Python 2 异常处理 (try 和 except),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Python 2 异常处理 [try 和 except]
- 1. Example 1
- 2. Example 2
- 3. Example 3
- References
异常处理使程序能检测错误,处理它们,然后继续运行。当试图用一个数除以零时,就会发生 ZeroDivisionError。
1. Example 1
#!/usr/bin/env python
# -*- coding: utf-8 -*-from __future__ import absolute_import
from __future__ import print_function
from __future__ import divisionimport os
import sys
import numpy as np
import tensorflow as tfsys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")def forever(divisor):return 42 / divisorprint(forever(2))
print(forever(12))
print(forever(0))
print(forever(1))
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
21.0
3.5
Traceback (most recent call last):File "/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py", line 27, in <module>print(forever(0))File "/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py", line 22, in foreverreturn 42 / divisor
ZeroDivisionError: division by zeroProcess finished with exit code 1
根据错误信息中给出的行号,我们知道 forever() 中的 return 语句导致了一个错误。错误可以由 try 和 except 语句来处理。那些可能出错的语句被放在 try 子句中。如果错误发生,程序执行就转到接下来的 except 子句开始处。可以将前面除数为零的代码放在一个 try 子句中,让 except 子句包含代码,来处理该错误发生时应该做的事。
2. Example 2
#!/usr/bin/env python
# -*- coding: utf-8 -*-from __future__ import absolute_import
from __future__ import print_function
from __future__ import divisionimport os
import sys
import numpy as np
import tensorflow as tfsys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")def forever(divisor):try:return 42 / divisorexcept ZeroDivisionError:print('Error: Invalid argument.')print(forever(2))
print(forever(12))
print(forever(0))
print(forever(1))
如果在 try 子句中的代码导致一个错误,程序执行就立即转到 except 子句的代码。在运行那些代码之后,执行照常继续。
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
21.0
3.5
Error: Invalid argument.
None
42.0Process finished with exit code 0
在函数调用中的 try 语句块中, 发生的所有错误都会被捕捉。请考虑以下程序,它的做法不一样, 将 forever() 调用放在语句块中。
3. Example 3
#!/usr/bin/env python
# -*- coding: utf-8 -*-from __future__ import absolute_import
from __future__ import print_function
from __future__ import divisionimport os
import sys
import numpy as np
import tensorflow as tfsys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")def forever(divisor):return 42 / divisortry:print(forever(2))print(forever(12))print(forever(0))print(forever(1))except ZeroDivisionError:print('Error: Invalid argument.')
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
21.0
3.5
Error: Invalid argument.Process finished with exit code 0
print(forever(1)) 从未被执行是因为, 一旦执行跳到 except 子句的代码, 就不会回到 try 子句。 它会继续照常向下执行。
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] (美) Al Sweigart (斯维加特) 著;王海鹏 译. Python编程快速上手:让繁琐工作自动化[M]. 北京:人民邮电出版社, 2016. 1-391
这篇关于Python 2 异常处理 (try 和 except)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!