What is the exception handling Try/Except operation in python
What this article shares to you is about the exception handling Try/Except operation in python. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Learn about exception handling in python today.
First, create a new text document testinfo.txt on the desktop for exception testing:
Then create a new python file named py3_exception.py, and write the operation code in this file:
# in order not to affect the run result, # I commented out the code that caused the exception error # if you need to test, # uncomment it
# first we open a non-existent file # f = open ('test_info.txt') # there will be an exception # FileNotFoundError: [Errno 2] # No such file or directory:' test_info.txt'# then put the above code in the exception handling code block # run code try: F = open ('test_info.txt') except Exception: print (' file does not exist!') # abnormal information is captured to print # the file does not exist! # where Exception is the base class of common errors # the exception that occurs when the file does not exist at the beginning is FileNotFoundError# is a subclass of Exception # next take a look at the following example: try: # Open an existing file, # the line of code will run normally f = open ('testinfo.txt') # declare a variable to assign an undefined value no_exist_var = bad_dataexcept Exception: print (' file does not exist!') # caught exception information, but the prompt message is not clear enough # easy to cause misunderstanding # modified as follows: try: F = open ('testinfo.txt') # declare a variable to assign an undefined value # no_exist_var = bad_dataexcept FileNotFoundError: print (' file does not exist!') # when we run the above code, we will throw out the specific exception information # NameError: name 'bad_data' is not defined # We adjust the above code try: F = open (' testinfo.txt') # declare a variable to assign an undefined value no_exist_var = bad_dataexcept FileNotFoundError: print ('file does not exist!') Except Exception: print ('there is an exception!') # next, let's print the details of the exception try: F = open ('test_info.txt') except FileNotFoundError as e: print (e) except Exception as e: print (e) # print result No such file or directory:' test_info.txt'
# if no exception occurs # you can use else to do some code processing logic try: F = open ('testinfo.txt') except FileNotFoundError as e: print (e) except Exception as e: print (e) else: print (f.read ()) f.close () # print file content: life is too short, you need python
# finale code block # try: F = open ('test_info.txt') # No except FileNotFoundError as e exists in the file: print (e) except Exception as e: print (e) else: print (f.read ()) f.close () finally: print (' execute finallycodes') # using raise to throw an exception # can be an exception defined by yourself: F = Open ('testinfo.txt') # file exists if f.name = =' testinfo.txt': raise Exceptionexcept FileNotFoundError as e: print (e) except Exception as e: print ("error!!") Else: print (f.read ()) f.close () finally: print ('execute finallychecking')
The result of running the above code:
The file does not exist! The file does not exist! Something unusual has happened! [Errno 2] No such file or directory: 'test_info.txt' 's life is too short to need python [Errno 2] No such file or directory:' test_info.txt' to finalise! It's a mistake! Execute finallyweights! The above is what the exception handling Try/Except operation in python is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.