'''
异常是错误发出的信号,一旦程序出错,就会产生一个异常,应用程序未处理该异常,异常便会抛出,程序随之终止
'''
# x
'''
NameError: name 'x' is not defined
'''
# if 1>2
# pass
'''
语法错误应该在程序运行前修正
SyntaxError: invalid syntax
'''
# l=[]
# l[10000]
'''
逻辑错误
IndexError: list index out of range
'''
# class Foo:
# pass
# Foo.x
'''
AttributeError: type object 'Foo' has no attribute 'x'
'''
# k={'x':1}
# k['y']
'''
KeyError: 'y'
'''
# 1/0
'''
ZeroDivisionError: division by zero
'''
# for i in 3:
# print(i)
'''
TypeError: 'int' object is not iterable
'''
# age=input('>>:')
# age=int(age)
'''
ValueError: invalid literal for int() with base 10: 'rrqef'
'''
# print('===>start<===')
# l=[]
# print(l[100])
# print('===>1')
# print('===>2')
# print('===>3')
# print('===>end<===')
'''
IndexError: list index out of range
'''
# print('===>start<===')
# try:
# l=[]
# print(l[100])
# print('===>1')
# print('===>2')
# print('===>3')
# except KeyError:
# pass
# print('===>end<===')
'''
IndexError: list index out of range
'''
# print('===>start<===')
# try:
# l=[]
# print(l[100])
# print('===>1')
# print('===>2')
# print('===>3')
# except IndexError:
# pass
# print('===>end<===')
'''
===>start<===
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# print(l[100])
# print('===>1')
# print('===>2')
# print('===>3')
# except IndexError as e:
# print('>>:',e)
# print('===>end<===')
'''
===>start<===
>>: list index out of range
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# print(l[100])
# print('===>1')
# d=[]
# d['k']
# print('===>2')
# print('===>3')
# except IndexError as e:
# print('>>:',e)
# print('===>end<===')
'''
===>start<===
>>: list index out of range
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# print('===>1')
# d = {}
# d['k']
# print('===>2')
# print('===>3')
# except IndexError as e:
# print('>>:',e)
# print('===>end<===')
'''
KeyError: 'k'
'''
# print('===>start<===')
# try:
# l=[]
# print('===>1')
# d = {}
# d['k']
# print('===>2')
# print('===>3')
# except IndexError as e:
# print('>>:',e)
# except KeyError as e:
# print('>>>:', e)
# print('===>end<===')
'''
===>start<===
===>1
>>>: 'k'
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# print(l[100])
# print('===>1')
# d = {}
# d['k']
# print('===>2')
# print('===>3')
# except Exception as e:
# print('万能异常:',e)
# print('===>end<===')
'''
===>start<===
万能异常: list index out of range
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# print('===>1')
# d = {}
# d['k']
# print('===>2')
# print('===>3')
# except Exception as e:
# print('万能异常:',e)
# print('===>end<===')
'''
===>start<===
===>1
万能异常: 'k'
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# print('===>1')
# d = {}
# d['k']
# print('===>2')
# print('===>3')
# except IndexError as e:
# print('>>:',e)
# except KeyError as e:
# print('>>>:', e)
# except Exception as e:
# print('万能异常:',e)
# print('===>end<===')
'''
===>start<===
===>1
>>>: 'k'
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# d={}
# except IndexError:
# pass
# except KeyError:
# pass
# except Exception as e:
# print('万能异常:',e)
# else:
# print('没有异常发生的时候触发')
# print('===>end<===')
'''
===>start<===
没有异常发生的时候触发
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# d={}
# except IndexError:
# pass
# except KeyError:
# pass
# except Exception as e:
# print('万能异常:',e)
# else:
# print('没有异常发生的时候触发')
# finally:
# print('有没有异常都触发')
# print('===>end<===')
'''
===>start<===
没有异常发生的时候触发
有没有异常都触发
===>end<===
'''
# print('===>start<===')
# try:
# l=[]
# d={}
# d['k']
# except IndexError:
# pass
# except KeyError:
# pass
# except Exception as e:
# print('万能异常:',e)
# else:
# print('没有异常发生的时候触发')
# finally:
# print('有没有异常都触发')
# print('===>end<===')
'''
===>start<===
有没有异常都触发
===>end<===
'''
'''
try:
conn=connect('1.1.1.1',9527)
conn.execute('selfxnldsmvoslnc')
finally:
conn.close()
'''
# stus=['egon','rambo','xxx']
# if len(stus) == 0:
# raise TypeError
# print('>>:')
'''
>>:
'''
# stus=[]
# if len(stus) == 0:
# raise TypeError
# print('>>:')
'''
TypeError
'''
# ip_list=['1,1,1,1','1,1,1,2','1,1,1,3']
# assert len(ip_list)>0
# print('取出IP地址,验证可用性')
'''
取出IP地址,验证可用性
'''
# ip_list=[]
# assert len(ip_list)>0
# print('取出IP地址,验证可用性')
'''
AssertionError
'''
# class MyException(BaseException):
# def __init__(self,msg):
# super(MyException,self).__init__()
# self.msg=msg
# raise MyException('类型错误')
'''
__main__.MyException
'''
# class MyException(BaseException):
# def __init__(self,msg):
# super(MyException,self).__init__()
# self.msg=msg
# def __str__(self):
# return '<%s>'%self.msg
# raise MyException('类型错误')
'''
__main__.MyException: <类型错误>
'''