An introduction to five exception handling mechanisms in python

  • 2020-04-02 14:01:38
  • OfStack

Ever since I started learning programming a few years ago, I have been afraid and repelling of exception handling in programs. That's because I don't know. In this attack on python, I first listed some of my most feared and unfamiliar pieces of content, including "exception handling".

Dive into Python doesn't specifically cover exception handling, just a little bit when used in the example. Today, download Learn Python and go straight to the exception handling section. This section has four chapters. The first chapter explains the general use of exception handling, and the following chapters discuss its mechanism in depth. I've only read the first chapter so far. Learn to use it first, and then expand on it if necessary.

Python mainly supports five exception mechanisms, one by one.

The default exception handler


s = 'Hello girl!'
print s[100]
print 'continue'

If we don't take any precautions against exceptions, an exception occurs during execution of the program, which interrupts the program, invokes python's default exception handler, and outputs the exception information on the terminal. In this case, line 3 will not execute.

The try... Except,


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

When the program runs to the second sentence, it finds a try statement, enters a try block for execution, an exception occurs, and goes back to the try statement layer to see if there is an except statement. After the except statement is found, the custom exception handler is called. After except handles the exception, the program continues to execute. In this case, the last two print statements are executed.

Except can also be null, meaning any type of exception is caught.

The try... The finally


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

A finally statement indicates that the statement in finally is executed whether an exception occurs or not. However, because there is no except processor, the program breaks when the finally completes. In this case, print 2 will execute, and print 1 will not execute. If there are no exceptions in the try statement, all three prints execute.

assert


assert False,'error...'
print 'continue'

This statement first determines whether the statement following an assert is True or False. If it is True, it continues to execute print. If it is False, it interrupts the program and invokes the default exception handler. In this case, the program is interrupted, an error is prompted, and the following print is not executed.

With... The as


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

When we usually use a stream object like a file, it is troublesome to call the close method to close it after using it. Here with... The as statement provides a very convenient alternative: open opens the file and assigns the returned file stream object to f, which is then used in the with block. When the with block is finished, the file is automatically closed, hidden.

If an exception occurs in the with statement or in a statement block, the default exception handler is invoked, but the file closes normally.

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

There is a lot of detail in the book, and in addition to what I mentioned above, there is a lot of useful additional information, such as try.. Except,.. The finally.. Else can be used in conjunction, such as custom exception classes. It is not listed here, for details, please refer to the introduction in this book.


Related articles: