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
Nesta página do site você pode assistir ao vídeo on-line How to Handle Exceptions in Python - 60 Seconds to Code duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Faisal Akhtar 14 Dezembro 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 53 vezes e gostou 1 espectadores. Boa visualização!