Python Exception handling

Source: Internet
Author: User

Python Exception handling

    • Kinds of Exception types

      • What is an exception type
        • Print # variable A is not defined # Nameerror:name ' A ' isn't defined  # Nameerror is an exception type

        

    • common types of anomalies
      • #common types of anomalies#Attributeerror attempts to access a tree that does not have an object, such as foo.x, but Foo has no attribute x#IOError input/output exception; Basically, the file cannot be opened#Importerror cannot introduce modules or packages; it is basically a path problem or a name error#indentationerror syntax error (subclass); Code not aligned correctly#indexerror Subscript Index is out of sequence boundary, for example, when X has only three elements, but tries to access X[5]#Keyerror attempts to access keys that do not exist in the dictionary#Keyboardinterrupt CTRL + C is pressed#Nameerror using a variable that has not been assigned to an object#syntaxerror python code is illegal, code can not compile (personally think this is a syntax error, write wrong)#TypeError Incoming object types are not compliant with the requirements#Unboundlocalerror attempts to access a local variable that has not yet been set, basically because another global variable with the same name causes you to think that you are accessing it#ValueError Pass in a value that is not expected by the caller, even if the value is of the correct type
      • Arithmeticerrorassertionerrorattributeerrorbaseexceptionbuffererrorbyteswarningdeprecationwarningenvironmenterroreoferror Exceptionfloatingpointerrorfuturewarninggeneratorexitimporterrorimportwarningindentationerrorindexerrorioerrorkeyboardint Erruptkeyerrorlookuperrormemoryerrornameerrornotimplementederroroserroroverflowerrorpendingdeprecationwarningreferenceerr Orruntimeerrorruntimewarningstandarderrorstopiterationsyntaxerrorsyntaxwarningsystemerrorsystemexittaberrortypeerrorunbou Ndlocalerrorunicodedecodeerrorunicodeencodeerrorunicodeerrorunicodetranslateerrorunicodewarninguserwarningvalueerrorwarni Ngzerodivisionerror
        Extended Exception types

    • Exception handling

    • Exception Handling Methods
      • using the IF-judgement
        • Num1=input ('>>:')#Enter a string to tryInt (NUM1) num1=input ('>>:')#Enter a string to tryifnum1.isdigit (): Int (NUM1)#Our Orthodox program is here, and the rest belongs to the exception-handling category .elifnum1.isspace ():Print('Enter a space and execute my logic here.')elifLen (num1) = =0:Print('the input is empty, just execute my logic here.')Else:    Print('other situations, execute my logic here.')" "Problem One: We only add exception handling for the first piece of code by using if, but these if are not related to your code logic, so your code will not be easy to read because of poor readability. This is just a small logic in our code, and if there is a lot of logic like that, Then every time we need to judge these things, we'll invert our code to be very verbose. " "

Python Exception handling

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.