Getting started with Python--18--exceptions and try,except statements

Source: Internet
Author: User

Python Standard Exception Summary



Assertionerror Assertion statement (assert) failed
Attributeerror Attempting to access an unknown object property
Eoferror User input file End flag EOF (CTRL+D)
Floatingpointerror Floating-point calculation error
Generatorexit When the Generator.close () method is called
Importerror When the import module fails
Indexerror Index out of range of sequence
Keyerror Find a keyword that does not exist in the dictionary
Keyboardinterrupt User input interrupt key (CTRL + C)
Memoryerror Memory overflow (can free memory by deleting objects)
Nameerror Try to access a non-existent variable
Notimplementederror Methods that have not been implemented
OSError An exception generated by the operating system (for example, opening a file that does not exist)
Overflowerror Numeric operation exceeds maximum limit
Referenceerror Weak references (weak reference) attempt to access an object that has been reclaimed by the garbage collection mechanism
RuntimeError General run-time errors
Stopiteration There are no more values for iterators
SyntaxError Syntax error for Python
Indentationerror Indentation Error
Taberror Use the tab and Space mix
Systemerror Python Compiler system error
Systemexit Python compiler process is closed
TypeError Invalid operations between different types
Unboundlocalerror Access an uninitialized local variable (subclass of Nameerror)
Unicodeerror Unicode-related errors (subclasses of ValueError)
Unicodeencodeerror Error in Unicode encoding (subclass of Unicodeerror)
Unicodedecodeerror Error in Unicode decoding (subclass of Unicodeerror)
Unicodetranslateerror Error in Unicode conversion (subclass of Unicodeerror)
ValueError Invalid parameter passed in
Zerodivisionerror Divide by zero



The following is a hierarchy of Python built-in exception classes:

Baseexception
+--Systemexit
+--Keyboardinterrupt
+--Generatorexit
+--Exception
+--stopiteration
+--Arithmeticerror
| +--Floatingpointerror
| +--Overflowerror
| +--Zerodivisionerror
+--Assertionerror
+--Attributeerror
+--Buffererror
+--Eoferror
+--Importerror
+--Lookuperror
| +--Indexerror
| +--Keyerror
+--Memoryerror
+--Nameerror
| +--Unboundlocalerror
+--OSError
| +--Blockingioerror
| +--Childprocesserror
| +--Connectionerror
|    | +--Brokenpipeerror
|    | +--Connectionabortederror
|    | +--Connectionrefusederror
|    | +--Connectionreseterror
| +--Fileexistserror
| +--Filenotfounderror
| +--Interruptederror
| +--Isadirectoryerror
| +--Notadirectoryerror
| +--Permissionerror
| +--Processlookuperror
| +--Timeouterror
+--Referenceerror
+--RuntimeError
| +--Notimplementederror
+--SyntaxError
| +--Indentationerror
| +--Taberror
+--Systemerror
+--TypeError
+--ValueError
| +--Unicodeerror
| +--Unicodedecodeerror
| +--Unicodeencodeerror
| +--Unicodetranslateerror
+--Warning
+--deprecationwarning
+--pendingdeprecationwarning
+--runtimewarning
+--syntaxwarning
+--userwarning
+--futurewarning
+--importwarning
+--unicodewarning
+--byteswarning
+--resourcewarning

With try:

Operation

Except Exception[as result]:

Print (' wrong ')

Give some chestnuts and usage:

Try

f = open (' Why am I a file '. txt ')

Print (F.read ())

F.close ()
Except OSError:

Print (' File Error! ') #运行一下, if there is a oserror error in front of you, the message will be output

Try

f = open (' Why am I a file '. txt ')

Print (F.read ())

F.close ()
Except OSError as Reult:

Print (' File Error! The reason is: ' +str (Result) #运行一下, if the preceding is a oserror error, here will output this message plus error information

A try statement can also connect multiple except exceptions:

Try

Sum =1+ ' 1 '

f = open (' Why am I a file '. txt ')

Print (F.read ())

F.close ()
Except OSError as Reult:

Print (' File Error! The reason is: ' +str (Result) #运行一下, if the preceding is a oserror error, here will output this message plus error information

Except TypeError as result:

Print (' wrong type ')

The Try statement plus the finally:

Try

Sum =1+ ' 1 '

f = open (' Why am I a file '. txt ')

Print (F.read ())

Except OSError as Reult:

Print (' File Error! The reason is: ' +str (Result) #运行一下, if the preceding is a oserror error, here will output this message plus error information

Finally

F.close () #finally语句是无论报不报错都要执行的语句

Getting started with Python--18--exceptions and try,except statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.