#This example is a separate processing of file exceptions and type exceptionsTry: Sum= 1+'1'F=open ('Wo.txt') Print(F.read ()) F.close ()exceptOSError as Reason:Print('file Error, error reason:'+str (reason))exceptTypeError as Reason:Print('type error, error reason:'+str (reason))#This example is a simultaneous processing of file exceptions and type exceptionsTry: Sum= 1+'1'F=open ('Wo.txt') Print(F.read ()) F.close ()except(Oserror,typeerror) as reason:Print('Error:'+str (reason))#This method is for all types of error processing, but do not know what the cause of the error, resulting in the programmer can not handleTry: Sum= 1+'1'F=open ('Wo.txt') Print(F.read ()) F.close ()except: Print('an error.')#This method is for all types of error processing, but do not know what the cause of the error, resulting in the programmer can not handleTry: F=open ('Wo.txt','W') Print(F.write ('haha haha')) Sum= 1+'1'except: Print('an error.')finally: F.close ()
Python, exception handling