Python2异常处理(try和except)
Python 2 异常处理 (try 和 except)
异常处理使程序能检测错误,处理它们,然后继续运⾏。当试图⽤⼀个数除以零时,就会发⽣ ZeroDivisionError。
1. example 1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.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 / divisor
print(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 forever
return 42 / divisor
ZeroDivisionError: division by zero
Process finished with exit code 1
根据错误信息中给出的⾏号,我们知道 forever() 中的 return 语句导致了⼀个错误。错误可以由 try 和 except 语句来处理。那些可能出错的语句被放在 try ⼦句中。如果错误发⽣,程序执⾏就转到接下来的 except ⼦句开始处。可以将前⾯除数为零的代码放在⼀个 try ⼦句中,让 except ⼦句包含代码,来处理该错误发⽣时应该做的事。
2. example 2
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.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 / divisor
except 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.0
Process finished with exit code 0
在函数调⽤中的 try 语句块中, 发⽣的所有错误都会被捕捉。请考虑以下程序,它的做法不⼀样, 将 forever() 调⽤放在语句块中。
3. example 3
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.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 / divisor
try:
print(forever(2))
print(forever(12))
print(forever(0))
print(forever(1))
except ZeroDivisionError:
python的try和except用法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] (美) Al Sweigart (斯维加特) 著;王海鹏 译. Python编程快速上⼿:让繁琐⼯作⾃动化[M]. 北京:⼈民邮电出版社, 2016. 1-391
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论