Python basic syntax-exception handling

Source: Internet
Author: User

Basic syntax

In the programming process in order to increase the friendliness of the program in the event of a bug will not be displayed to the user error message, but the reality of a hint of the page, popular is not to let users see the big Yellow Pages!

Try :     Pass except Exception as E:     Pass

Requirement: Add two numbers entered by the user

 whileTrue:num1= Raw_input ('NUM1:') num2= Raw_input ('num2:')    Try: Num1=Int (NUM1) num2=Int (num2) result= Num1 +num2exceptException, E:Print 'An exception occurred with the following information:'        PrintE
View Codetype of Exception

There are so many exceptions in Python that each exception is dedicated to handling an exception!

Attributeerror attempts to access a tree that does not have an object, such as foo.x, but Foo does not have a property xioerror input / output exception; it is basically impossible to open the file Importerror the module or package cannot be introduced is basically a path problem or name error Indentationerror syntax error (subclass); The code is not aligned correctly indexerror the subscript index exceeds the sequence boundary, for example, when X has only three elements, it attempts to access x[5]keyerror Attempting to access a key that does not exist in the dictionary keyboardinterrupt Ctrl+c is pressed nameerror use a variable that has not been assigned to the object SyntaxError Python code is illegal, the code cannot compile (personally think this is a syntax error, Wrong) TypeError the incoming object type is not compliant with the requirements Unboundlocalerror attempts to access a local variable that is not yet set, basically because another global variable with the same name causes you to think that you are accessing it valueerror a value that the caller does not expect , even if the type of the value is correct
Common Exceptions
Arithmeticerrorassertionerrorattributeerrorbaseexceptionbuffererrorbyteswarningdeprecationwarningenvironmenterroreoferror Exceptionfloatingpointerrorfuturewarninggeneratorexitimporterrorimportwarningindentationerrorindexerrorioerrorkeyboardint Erruptkeyerrorlookuperrormemoryerrornameerrornotimplementederroroserroroverflowerrorpendingdeprecationwarningreferenceerr Orruntimeerrorruntimewarningstandarderrorstopiterationsyntaxerrorsyntaxwarningsystemerrorsystemexittaberrortypeerrorunbou Ndlocalerrorunicodedecodeerrorunicodeencodeerrorunicodeerrorunicodetranslateerrorunicodewarninguserwarningvalueerrorwarni Ngzerodivisionerror
more Exceptionsexception Other Structure
Try :     # main code block    Pass except keyerror,e:     # exception, the block    is executed Pass Else :     # when the main code block finishes executing, the block    is executed Pass finally :     # whether the exception or not, the final execution of the block    Pass
proactively triggering exceptions
Try :     # proactively triggering exceptions    Raise Exception (' wrong ...  ')except  Exception as E:    print E
Custom exceptions
classOldboyerror (Exception):#Defining Exception Classes    def __init__(Self, msg): Self.message=msgdef __str__(self):returnSelf.messageTry:    RaiseOldboyerror ('Error')#proactively triggering exceptionsexceptOldboyerror as E:Print(e)
Assertion
# Assert Condition assert assert 1 = = 2
assert conditions, assertions, used to force the user to obey, do not obey the error, can be replenishment, generally do not replenish goods assert 1==1Print # normal printing 123assert 1==2print# Direct Error
View Code

Python basic syntax-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.