https://bit.ly/3mfKcHH
Welcome to 60 seconds to code where I show you how to code in 60 seconds or less.
Today you will learn about exception handling. Exceptions are errors that occur in your code. If you know in advance that a piece of code might cause an error, you can gracefully handle the exception. For example, if you expect a file to exist but there is a possibility that it has been corrupted.
Step 1- Create a file called testfile.txt and enter some text in the file.
Step 2 – Create a file called exceptionhandling.py and in line 1 type try:. This try block will contain the code that you suspect might cause an error.
Step 3 – In line 2 type f = open('testfile.txt')
Step 4 – In line 3 type except FileNotFoundError as e: in line 4 type print(e). This except block will execute if the file you are looking to open does not exist.
Step 5 – In line 5 type else: . This code block will execute if your try code block does not cause an error. In line 6 type print(f.read()) and in line 7 type f.close()
Step 6 – Run your code to successfully print the contents of the file. Then try introducing an error in your code by changing the file name in the open function call and running the code again.
The code is available in a link in the description.
Congratulations, you have learned about exception handing. For your homework, create a different error in your code and write a different except block to catch that error and try to code a little every day because code is life.
Learn #exceptions and #exceptionhandling in #python #100DaysOfCode #301DaysofCode #60secondstocode
https://bit.ly/3mfKcHH
On this page of the site you can watch the video online How to Handle Exceptions in Python - 60 Seconds to Code with a duration of hours minute second in good quality, which was uploaded by the user Faisal Akhtar 14 December 2020, share the link with friends and acquaintances, this video has already been watched 53 times on youtube and it was liked by 1 viewers. Enjoy your viewing!