1 #!/usr/bin/env python2 #Coding:utf-8 # Encoding3 4 'a test module'5 6 __author__='Sunflower' 7 8 9 #exception Error TypeTen #assertionerror Segmentation Errors One Try: AMy_list =['Zhangsan'] - assertLen (my_list) >0 - My_list.pop () the assertLen (my_list) >0 - exceptassertionerror as Reason: - Print('Segmentation Errors'+str (reason)) - + - #Indexerror + #keyerror Dictionary Access # my_dict.get (' four ') A #IOError input/output operation error at #OSError Operating system error - #overflowerror operation exceeded limit - #SyntaxError # syntax - #TraceError - #zerodivisionerror except 0 - #TypeError Type Error in - #Debug Try ... except ... finally raise to Try:#try to error first print, followed by instruction not +f = open ('I am a file. txt','R') - PrintF.read () the f.close () * exceptIOError as Reason: $ Print('File Error'+str (reason))Panax Notoginseng finally:#Absolute Execution - f.close () the + RaiseIOError ('File Error') A the #Else + #If ... else ... Or... Or ... - #While...else ... Finish the job ... Do not finish ... $ #find greatest common divisor $ defshow_factor (num): -Count = num//2#except 2 - whileCount > 1: the ifNum% Count = =0: - Print('%d The largest approximate number is%d'%(num, count))Wuyi Break theCount-= 1 - Else: Wu Print('%d is the prime number!! '% num)#Execution is all done and executed. - Aboutnum = Int (33) $ Print(Show_factor (num)) - #try...else ... - Try: -int'ABC') A exceptvalueerror as Reason: + Print('something went wrong.'+str (reason)) the Else: - Print('There is no error.') $ the # with the Try: theOpen'Data.txt','W') the forEach_lineinchF: - Print(Each_line) in exceptIOError: the Print('something went wrong.') the finally: AboutF.close ()
Python Learning note Five: errors, debugging, and testing