Python study Note 9: exception, python study Note 9:
I. Syntax
#! /Usr/bin/pythonfilename = 'hello' # try using t finally demotry: open('abc.txt ') print hellospont IOError, msg: print 'The file not exist' handle T NameError, msg: print 'Hello not defined' finally: print 'end' # throw exceptionif filename = "hello": raise TypeError ('Nothing ')Ii. Common exception types
1. AssertionError: assert statement failed
2. AttributeError: attempts to access a property that is not present in an object
3. IOError: the input and output are abnormal. the file cannot be opened.
4. ImportError: the module or package cannot be introduced, which is basically a path problem.
5. IndentationError: syntax error. The code is not correctly aligned.
6. IndexError: The subscript index exceeds the sequence boundary.
7. KeyError: attempts to access a key that does not exist in your dictionary
8. KeyboardInterrupt: Ctrl + C is pressed
9. NameError: Use a declared variable.
10. SyntaxError: the code logic syntax is incorrect and cannot be executed.
12. TypeError: the input object type does not meet the requirements.
13. UnboundLocalError: Try to ask a global variable that has not been set, basically because there is another global variable with the same name.
14. ValueError: input an unexpected value, even if the type is correct