Python object-oriented Miscellaneous (postscript), python object-oriented postscript

Source: Internet
Author: User

Python object-oriented Miscellaneous (postscript), python object-oriented postscript
Exception HandlingWhat is an exception:When debugging a program, we often fail to run the program due to various error messages. Exceptions are also an object used to indicate errors. We can capture exceptions and handle them accordingly to increase code friendliness. Exception Handling: You can use the try/try t statement to capture exceptions. When a try statement is encountered during code running, the code is executed first. If the statement is executed normally, the Skip t statement is skipped and the next execution is continued. if an exception is triggered, the Skip t statement is executed, exception Handling is performed by the explain T statement.

try:    passexcept Exception:    pass
Exception types:There are many exceptions in python, and each exception can be handled by special exceptions. Only some common exceptions are listed here.
AttributeError attempts to access a tree without an object, such as foo. x, but foo has no attribute xIOError input/output exception. Basically, the file ImportError cannot be opened and modules or packages cannot be introduced; basically, it is a path problem or name error. IndentationError syntax error (subclass). The Code does not correctly align the IndexError subscript index beyond the sequence boundary. For example, if x has only three elements, however, I tried to access x [5] KeyError. I tried to access the key KeyboardInterrupt Ctrl + C that does not exist in the dictionary. I was pressed NameError TO USE THE SyntaxError Syntax of a variable that has not been assigned an object. The TypeError passed in the object type and it does not conform to UnboundLocalError and tries to access a local variable that has not been set yet, basically, a global variable with the same name leads you to think that you are accessing its ValueError and passing in an unexpected value from the caller, even if the value type is correct FloatingPointError floating point calculation error OverflowError the value calculation exceeds the maximum limit RuntimeError is a general runtime error

When an exception type is specified, only corresponding exceptions can be handled. Exceptions of other types cannot be caught.

Dir = {'k1 ': 'v1', 'k2': 'v2'} try: a = dir ['k3 '] print (a) failed t KeyError: # print ('no such key')> no such key
A = 'zhang' try: int (a) failed t TypeError: # The specified exception type is incorrect and the print ('error') exception cannot be caught >>> traceback (most recent call last): File "C:/Users/issuser/PycharmProjects/Python/day9/socket client. py ", line 11, in <module> int (a) ValueError: invalid literal for int () with base 10: 'zhang'
When writing a program, you sometimes need to consider handling multiple exceptions. You can write multiple TTS. However, you must note that the range of matching exceptions is small to large, so as not to capture the corresponding exceptions.
Universal Exception: Another Exception in python can capture all exceptions. It is an Exception that can capture all exceptions.
A = 'zhang' try: int (a) failed t TypeError: print ('Type error') failed t Exception: # All exceptions can be caught to ensure that the program runs print ('other error ')

In the exception, we can also use else. If no exception is triggered, we can continue to execute the else code. If an exception is triggered, the processing exception is not executed.

Try: # main code block passexcept KeyError, e: # When an exception occurs, execute the block passelse: # after the main code block is executed, execute the block passfinally: # whether the exception or not, and finally execute the block pass

In the try/finally format, after the try block is executed, the finally block is executed no matter whether the exception is set or not.

Try: # main code block passexcept KeyError: # When an exception occurs, execute the block passelse: # after the main code block is executed, execute the block passfinally: # whether it is abnormal or not, and finally execute the block pass

In addition to capturing code exceptions, you can also trigger exceptions actively (I really can't think of any birds)

Try: raise IndexError ('error') # raise actively triggers an exception, indicating the exception type 'ttypeerror: print ('Type error') failed t IndexError: # print ('index error')> index error

Assertions:

# Assert condition assert 1 = 1 # If the condition is true, null assert 1 = 2 # If the condition is false, an exception is returned.

 

Reflection

 

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.