Introduction to five exception handling mechanisms in python, and Exception Handling in python

Source: Internet
Author: User

Introduction to five exception handling mechanisms in python, and Exception Handling in python

Since learning programming a few years ago, I have been afraid and exclusive to program exception handling. This is because you do not know. In this attack on python, I first listed several pieces of content that I was most afraid of and unfamiliar with, which included "Exception Handling.

"Dive into Python" does not specifically introduce exception handling, but it is slightly explained in the example. Download Learn Python today and go directly to exception handling. This part has four chapters. The first chapter describes the general usage of exception handling, and the later chapter discusses its mechanism in depth. At present, I only read the first chapter. I learned how to use it first, and then read it more when necessary.

Python mainly supports five exception mechanisms, one by one.

Default exception Processor

Copy codeThe Code is as follows:
S = 'Hello girl! '
Print s [100]
Print 'continue'

If we do not prevent exceptions, the program will be interrupted when exceptions occur during program execution, the default exception processor of python will be called, and the exception information will be output on the terminal. In this case, the 3rd lines of code will not be executed.

Try... Except

Copy codeThe Code is as follows:
S = 'Hello girl! '
Try:
Print s [100]
Failed t IndexError:
Print 'error ...'
Print 'continue'

When the program executes 2nd sentences, it finds the try statement and enters the try statement block for execution. If an exception occurs, return to the try statement layer to check whether there is a limit t statement. After finding the handler t statement, the custom exception processor is called. When it completes the exception handling, the program continues to run. In this case, the last two print statements are executed.

The suffix t can also be blank to capture exceptions of any type.

Try... Finally

Copy codeThe Code is as follows:
S = 'Hello girl! '
Try:
Print s [100]
Finally:
Print 'error ...'
Print 'continue'

The finally statement indicates that the statements in finally must be executed no matter whether an exception occurs or not. However, because there is no ipvt processor, the program is interrupted after finally execution. In this case, 2nd print will be executed, and 1st print will not be executed. If there is no exception in the try statement, all three prints will be executed.

Assert

Copy codeThe Code is as follows:
Assert False, 'error ...'
Print 'continue'

This statement first determines whether the statement followed by assert is True or False. If it is True, print is executed. If it is False, the program is interrupted and the default exception processor is called, at the same time, the prompt message after the assert statement comma is output. In this case, the program is interrupted and an error is prompted, and the subsequent print is not executed.

With... As

Copy codeThe Code is as follows:
With open('nothing.txt ', 'R') as f:
F. read ()
Print 2/0
Print 'continue'

When using stream objects similar to files, we need to call the close method to close them after use, which is very troublesome. Here... The as statement provides a very convenient alternative: open the file, assign the returned file stream object to f, and then use it in the with statement block. After the with statement block is completed, the file is automatically closed hidden.

If an exception occurs in the with statement or statement block, the default exception processor will be called for processing, but the file will still be closed normally.

In this case, an exception is thrown and the final print is not executed.

The description in the book is very detailed. In addition to the above mentioned, there are also a lot of useful additional information, such as try... try t... finally... else can be used together, such as custom exception classes. It is not listed here. For details, refer to the introduction in this book.


Is there an interrupt mechanism in python?

Pygame is a solution. In addition, you can understand the interface interaction.

The program can process multiple events because it has multiple threads to support multiple tasks at the same time. Generally, the structure of an interface program is as follows.

The main interface thread continuously receives and draws and processes keyboard messages in the window cyclically. But now the main interface thread is single-threaded. Computing capability is not strong. Therefore, we need to put some calculations in the background. However, in any case, moving is drawing, and it is impossible to receive keyboard input at the same time, but the speed is very fast and there is no intermediate interval.

Error Check in Python simple calculator

If raw_input () [0: 2] in ['add', 'sub']:
Do something
Else:
# Error

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.