标签:style blog http color io os 使用 ar for
>>> a=[0,1,2,3,4,5] >>>try: ... a[10] ... except IndexError: ... print ‘index error~~~~‘ ... index error~~~~
1 #!/usr/bin/python 2 import time 3 for i in range(50): 4 try: 5 print i 6 time.sleep(3) 7 print ‘~~~~‘ 8 except KeyboardInterrupt: 9 print ‘Do not stop me!‘ 10 print ‘+++++‘
1 #!/usr/bin/python 2 import time 3 dict={0:0,1:1,2:2,3:3,4:4,5:5} 4 input=int(raw_input(‘Please input a number:‘)) 5 try: 6 for num in range(input): 7 try: 8 print ‘number ‘,dict[num] 9 time.sleep(0.3) 10 except KeyboardInterrupt: 11 print ‘Do not stop me!‘ 12 except KeyError: 13 print ‘%s not exist‘%num
#!/usr/bin/python try: raise NameError raise IndexError except IndexError: print ‘index error!!!‘ except NameError: print ‘name error!!!‘
#!/usr/bin/python class myexcept(Exception): -- 定义异常 pass try: -- 触发 raise myexcept except myexcept: -- 捕获 print ‘My error!!!‘
1 #!/usr/bin/python 2 class myexcept(Exception): 3 pass 4 try: 5 pass 6 # raise myexcept 7 except myexcept: 8 print ‘My error!!!‘ 9 else: 10 print ‘No exception‘ 11 finally: 12 print ‘finally print~~~‘
#!/usr/bin/python try: pass raise Indexerror except : print ‘My error!!!‘ else: print ‘No exception‘ finally: print ‘finally print~~~‘
#!/usr/bin/python import sys try: pass raise IndexError,"line 5,please check it" except IndexError,data: print data
标签:style blog http color io os 使用 ar for
原文地址:http://www.cnblogs.com/kissdb/p/4009600.html