Python try except else finally instance
classAerror (Exception):"""Aerror---exception""" Print('Aerror')classBerror (Exception):"""Berror---exception""" Print('Berror')Try: #Raise Aerror #Raise BerrorAAAAA ()exceptAerror:Print("Get Aerror")exceptBerror:Print("Get Berror")except: Print("Get Exception")Else: Print("Do Else")finally: Print("finally")
1. The order in which the try/except/else/finally appears in the complete statement shown above must be try-->except x-->except-->else-->finally, That is, all except must precede else and finally, else (if any) must precede finally, and except x must precede except. Otherwise, a syntax error will occur.
2. In the complete statement above, the presence of the Else statement must be premised on the except X or except statement, and a syntax error is raised if the Else statement is used in a try block without a except statement. That is, else cannot be used in conjunction with try/finally.
3.try success in else, not successfully into the corresponding except;
4. Whether or not successful except X,except judgment statement will be executed, but do not enter; corresponding to the above procedure is Aerror Berror will print in either case, but get Aerror get Berror will print only if the corresponding exception is thrown.
5.finally all cases will be executed, can not write, write must be in all except else, etc.;
Python try except else finally