In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "python3 how to achieve operation file exception handling", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "python3 how to achieve operation file exception handling" this article.
When executing the python script, when it is running normally, errors may occur. The error type returned is the error message. We can execute the corresponding statements according to the error type returned. There are many error types, such as ValueError, TypeError, NameError, ZeroDivisionError.
1 、 try / except
Let's have a simple syntax, as follows:
While True: try: X = int (input ('Please enter a number:') break except ValueError: print ('you did not enter a number, please try again!')
The implementation results are as follows:
Please enter a number: d what you entered is not a number, please try again! Please enter a number: 3
The try statement works as follows:
First, execute the try clause (the statement between the keywords try and except).
If no exception occurs, the except clause is ignored and ends after the try clause is executed.
If an exception occurs during the execution of the try clause, the rest of the try clause is ignored, and if the exception type is the same as the exception type defined after except, the clause under the except keyword is executed.
If an exception does not match any except, the exception will be passed to the upper try.
A try statement can contain multiple except clauses to handle different specific exceptions, but at most one except branch will be executed.
The handler will handle only the exceptions in the corresponding try clause, not those in other try handlers.
An except clause can handle multiple exceptions at the same time, and these exceptions are placed in parentheses to form a tuple, chestnut as follows:
Except (RuntimeError,TypeError,NameError,ValueError): pass
Note: pass means to ignore this exception and do not execute any statements.
The last except clause ignores the name of the exception, which is used as a wildcard. The chestnuts are as follows:
Try: F = open ('myfile.txt') s = f.readline () I = int (s.strip ()) except OSError as err: print ("OS error: {0}" .format (err)) except ValueError: print ("data cannot be converted to integer type!") Except: pass2 、 try / except / else
The try/except statement also has an optional else clause, which, if used, must be placed after all except clauses, which will be executed when there is no exception to the try clause.
The syntax format is as follows:
Try: # normally executed code block except exception type: # code block executed when an exception occurs else: # code block executed when there is no exception
The chestnuts are as follows:
While True: try: X = int (input ('Please enter a number:') # break except (ValueError,NameError,Exception): print ("you did not enter a number!") Else: print ('user input complete!')
Using the else clause is better than putting all the statements in the try clause, which avoids unexpected exceptions that except cannot catch.
Exception handling not only handles those exceptions that occur directly in the try clause, but also handles the functions called in the clause (or even indirectly called functions) to throw exceptions, as shown below:
Try: this_fails () except ZeroDivisionError as err: print ('Handling run-time error:', err)
In the chestnut above, if the error type in the function is: ZeroDivisionError, the following will be output:
Handling run-time error:err3 、 try / finally
The try / finally statement executes the final code whether it occurs or not.
Syntax format:
Try: # normally executed code block except exception type: # code block executed when an exception occurs else: # code block executed when there is no exception finally: # code block that executes regardless of whether there are exceptions or not
Examples are as follows:
While True: try: X = int (input ('Please enter a number:') # break except (ValueError,NameError,Exception): print ("you did not enter a number!") Else: print ('user input complete!') Finally: print ("this sentence is executed regardless of whether an exception occurs.") The above is all the contents of the article "how to implement exception handling of operation files in python3". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.