Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python exception handling

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Python exception handling example analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python exception handling example analysis" bar!

Exception handling

Exception handling is indispensable in project development. Exception handling helps people debug, making it easier for people to find the location of bug through richer information. Exception handling can also improve the fault tolerance of the program.

When we talked about loop objects earlier, we mentioned a StopIteration exception, which is an error when the loop object runs out of elements.

Let's take it as an example to illustrate basic exception handling.

A program that contains exceptions:

Re = iter (range (5)) for i in range: print re.next () print 'HaHaHaHa'

First, we define a loop object, re, which will loop five times, using one element of the sequence each time.

In the subsequent for loop, we manually call the next () function. When the loop reaches the sixth time, re.next () no longer returns the element, but instead throws the exception of (raise) StopIteration. The whole program will be interrupted.

We can modify the above exception program until there is no bug perfectly. On the other hand, if we know that there may be mistakes and possible types of errors when writing the program, we can define a "contingency plan" for this exception type.

Re = iter (range (5)) try: for i in range (100): print re.next () except StopIteration: print 'here is end', iprint 'HaHaHaHa'

In the try section, we put in the error-prone parts. We can follow except to show what the program should do if StopIteration occurs in the statement in the try section. If no exception occurs, the except section is skipped.

After that, the program will continue to run, rather than being completely interrupted.

The complete syntax structure is as follows:

Try:... except exception1:... except exception2:... except:... else:... finally:...

If an exception occurs in the try, the attribution of the exception is executed and the except is executed. The exception is compared layer by layer to see if it is exception1 and exception2..., until it is found and the statement in the corresponding except is executed. If there are no parameters after except, it means that all exception are handed over to this program. For example:

Try: print (axi2) except TypeError: print ("TypeError") except: print ("Not TypeError & Error noted")

Because an is not defined, it is NameError. The exception is eventually caught by the except: part of the program.

If the exception cannot be handed over to the appropriate object, the exception will continue to be thrown up until it is caught or causes the main program to report an error. For example, the following program

Def test_func (): try: M = 1Catch error in the main program 0 except NameError: print ("Catch NameError in the sub-function") try: test_func () except ZeroDivisionError: print ("Catch error in the main program")

Try...except... of subroutine The structure cannot handle the corresponding error divided by 0, so the error is thrown to the upper main program.

If there are no exceptions in the try, the except section skips and executes the statements in the else.

Finally is the last thing to do, whether there is an exception or not.

The process is as follows

Try- > exception-> except- > finally

Try- > No exception-> else- > finally

Throw an exception

We can also write an example of throwing an exception ourselves:

Print 'Lalala'raise StopIterationprint' '

This example does not have any practical significance. Just to illustrate the purpose of the raise statement.

StopIteration is a class. When an exception is thrown, there is automatically an intermediate link, which is to generate an object of StopIteration. What Python actually throws is this object. Of course, you can also generate your own objects:

Raise StopIteration () Thank you for your reading. The above is the content of "example Analysis of Python exception handling". After the study of this article, I believe you have a deeper understanding of the analysis of Python exception handling examples, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report