Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of python exception handling

Shulou Source: shulou.com Published: 2022-06-03 17:08:33 11月13日 Update

This article will explain in detail the example analysis of python exception handling. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Description

1. Execute the statement of try.

2. If there is any abnormality, look for the corresponding abnormal disease in the except statement to deal with it.

3. If there is no exception, execute the content of the else statement.

4. In the end, the finally statement is executed regardless of whether there is an exception or not.

Example

# simple exception case try: num = int (input ("Please input your number:") rst = 100/num print ("calculation result: {}" .format (rst)) except: print ("input error") # exit means to exit the program exit () Please input your number:0 input error # simple exception case # give the prompt try: num = int (input ("Please") Input your number: ") rst = 100/num print (" calculation result: {} ".format (rst)) # after catching an exception Instantiate the exception, the error message will be in the instance # pay attention to the following writing # the following statement is to catch the ZeroDivisionError exception and instantiate the instance eexcept ZeroDivisionError as e: print ("input error") print (e) # exit means to exit the program exit () about the example analysis of python exception handling, this article ends here. I hope the above content can be of some help to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Examples statements processing articles errors input examples analysis information content meaning more cases programs results good practical writing articles knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn Huawei Shulou Technology NVidia