python 之异常Exception 1
2018-03-26 11:07
363 查看
一、什么是异常:
file_name = input('请输入需要打开的文件名:')
f = open(file_name)
print('文件的内容是:')
for each in f:
print(each)#结果报错:FileNotFoundError: [Errno 2] No such file or directory: 'fff'
list1 = [123]
assert len(list1)>0#不会报错
list1.pop()#list1中没有元素了
assert len(list1)>0#结果:报错AssertionError
file_name = input('请输入需要打开的文件名:')
f = open(file_name)
print('文件的内容是:')
for each in f:
print(each)#结果报错:FileNotFoundError: [Errno 2] No such file or directory: 'fff'
i = 9/0#ZeroDivisionError: division by zero二、python标准异常总结 http://bbs.fishc.com/thread-45814-1-1.html #AssertionError 断言语句(assert)失败
list1 = [123]
assert len(list1)>0#不会报错
list1.pop()#list1中没有元素了
assert len(list1)>0#结果:报错AssertionError
#AttributeError 尝试访问未知的对象属性(python中包含一个变量名都是一个对象) #新建对象 list1 = [123] list1.cout()#列表不存在cout方法,因此报错AttributeError: 'list' object has no attribute 'cout'
#IndexError:索引的值超出了序列的范围 list1 = [1,2,3] list1[3]#结果报错:IndexError: list index out of range
#keyError 字典中查找一个不存在的关键字 dict1 = {'one':1,'two':2} print(dict1['one'])#结果:1 print(dict1['onene'])#报错:KeyError: 'onene'
相关文章推荐
- Selenium python 启动firefox浏览器的异常selenium.common.exceptions.WebDriverException: Message: 'geckodriver'
- Python中获取异常(Exception)信息
- python的异常Exception
- Python中获取异常(Exception)信息
- Python中的异常(Exception)
- Python 3语法小记(九) 异常 Exception
- Python中获取异常(Exception)信息
- Python 异常(Exception)
- Python中获取异常(Exception)信息
- Python中异常(Exception)的总结
- python Exception 异常处理
- Python 异常(Exception)
- 理解Python语言里的异常(Exception)
- 关于Python中异常(Exception)的汇总
- Python 3语法小记(九) 异常 Exception
- Python 3语法小记(九) 异常 Exception
- selenium+python之异常StaleElementReferenceException
- Python 异常 Exception
- Python,异常 exception
- Python 3语法小记(九) 异常 Exception