如何在Python中捕获OSError异常?
OSError用作os模块的错误类,并在特定于os的函数返回错误时引发。
我们可以按照以下方式重新编写给定的代码,以处理异常并了解其类型。
#foobar.py import os import sys try: for i in range(5): print i, os.ttyname(i) except Exception as e: print e print sys.exc_type
如果我们在Linux终端上运行此脚本
$ python foobar.py
我们得到以下输出
输出值
0 /dev/pts/0 1 /dev/pts/0 2 /dev/pts/0 3 [Errno 9] Bad file descriptor <type 'exceptions.OSError'>
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短