Try statements are typically paired with except and Finnaly. This is to give their own program to leave a back, if the corresponding set of error, you can "obedient while look", and then necessary when the auxiliary finnaly to a mandatory implementation.
A try can be with more than one except, after all, a program may be hidden in a lot of errors, natural error types will be many. This follows a number of except, there will be many disaster tolerance options, many of the except nature is from the top to the bottom of the order to take action.
For Instance:
F=["A", "F", "O", 7,4,9,3,64] #这是一个大杂烩, everything there is #
Try
F.sort () #把这个大杂烩排序, this is not possible because Python does not have the default alphabetical sort feature #
Print ("yes!")
Except TypeError:
F=[1,4,5,6,7,2,8,99]
F.sort ()
F.append ("That's right! ")
Aaa=f
Finally:
Print (AAA)
The results of the above program output are [1, 2, 4, 5, 6, 7, 8, right! '], because TypeError is present, executes the except statement, and then executes the finally statement.
For Instance:
F=["A", "F", "O", 7,4,9,3,64] #这是一个大杂烩, everything there is #
Try
F.sort () #把这个大杂烩排序, this is not possible because Python does not have the default alphabetical sort feature #
Print ("yes!")
Except OSError: #这里先检查是否存在OSError, if there is the execution of this one, the following will not look at the #
F=[7,9,3,5,6,100]
Print (f)
Except TypeError:
F=[1,4,5,6,7,2,8,99]
F.sort ()
F.append ("That's right! ")
Aaa=f
Finally:
Print (AAA)
=============================================== Split Line ==================================
If you need to add multiple error at once, you can use: Except (Oserror,syntaxerror,typeerror,zerodivisionerror,nameerror,Keyerror, indexerror,valueerror) And so on and so on, Python's exception kind is still very rich. This way, once the exception is found from the error in the setup, the corresponding operation is executed directly .
Of course, yards of farmers if their own initiative on the pole to find Error,python can also be done, please use "Raise + corresponding Error"
For Instance:
Try
Raise TypeError
Raise SyntaxError
Raise Keyboardinterrupt
Except (Typeerror,syntaxerror,keyboardinterrupt):
Print ("Ever since you got psychotic!" ")
Finally
Print ("I'm much easier than before!") ")
The output of this program is:
Ever since I got psychotic!
I'm a lot easier than before!
===================================== Split Line ===========================================
For instance:
Try
Print ("123")
Except: #不要轻易什么Error都不加就写这个, big bogey! #
Print ("456")
Else
Print ("789")
Finally
Print ("Jqka")
The output of this program is:
123
789
Jqka
Because there is no fault in the try statement, so the normal output, since there is no error, then the except statement skipped, and else is for the except statement, so it will output normally, eventually finally is dead and alive to output, so there will be such a result.
This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1728076
A little bit of Python's experience with try statements