Detailed explanation of five exception handling mechanisms in python

高洛峰
Release: 2017-03-17 15:55:52
Original
1731 people have browsed it

I started learning programming a few years ago. Until now, I have always been afraid and repelled by Exception handling in programs. The reason for this is due to lack of understanding. This time when I attack python, I first list the few things that I fear the most and are least familiar with. Among them is "Exception Handling".

"pe into Python" does not specifically introduce exception handling, but only briefly explains it when used in examples. Download "Learn Python" today and go directly to exception handling. This part has four chapters. The first chapter explains the general use of exception handling, and the subsequent chapters discuss its mechanism in depth. I have only read the first chapter so far. I want to learn how to use it first and then read more when necessary.

Python mainly supports five exception mechanisms, listed one by one.

Default exception handler

The code is as follows:

s = 'Hello girl!'
print s[100]
print 'continue'
Copy after login

If we do not take any precautions for exceptions, then if an exception occurs during program execution, the program will be interrupted. , calling python's default exception handler and outputting exception information in the terminal. In this case, line 3 of code will not be executed.

try…except

The code is as follows:

s = 'Hello girl!'
try:
 print s[100]
except IndexError:
 print 'error...'
print 'continue'
Copy after login

When the program executes to the second sentence, it finds the try statement, enters the try statement block for execution, an exception occurs, and returns to the try statement layer , look for whether there is an except statement behind it. After the except statement is found, this custom exception handler will be called. After except handles the exception, the program continues execution. In this case, the last two print statements will be executed.

except can also be left empty, indicating that any type of exception is caught.

try…finally

The code is as follows:

s = 'Hello girl!'
try:
 print s[100]
finally:
 print 'error...'
print 'continue'
Copy after login

The finally statement indicates that the statements in finally must be executed regardless of whether an exception occurs or not. However, since there is no except handler, the program is interrupted after finally execution. In this case, the second print will be executed, but the first print will not be executed. If there is no exception in the try statement, all three prints will be executed.

assert

The code is as follows:

assert False,'error...'
print 'continue'
Copy after login

This statement first determines whether the statement immediately following assert is True or False. If it is True, continue to execute print. If it is False The program is interrupted, the default exception handler is called, and the prompt information after the comma in the assert statement is output. In this case, the program is interrupted, an error is prompted, and subsequent prints are not executed.

with…as

The code is as follows:

with open('nothing.txt','r') as f:
 f.read()
 print 2/0
print 'continue'
Copy after login

When we usually use a file-like stream

object, we need to call the close method after use. , very troublesome. The with...as statement here provides a very convenient alternative: after open opens 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 will be closed automatically and hiddenly.

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

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


The above is the detailed content of Detailed explanation of five exception handling mechanisms in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!