Exceptions in Python: Execute Code ONLY when NO Exception is Raised (TRY ELSE in Python)

Publié le: 28 avril 2021
sur la chaîne: Digital Academy
533
21

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Exceptions in Python: Execute Code ONLY when NO Exception is Raised (ELSE)

In some situations, you might want to run a certain block of code, IF the code inside the try clause ran without any errors. For these cases, you can use the optional ELSE keyword with the TRY statement in Python, to define a block of code to be executed, if and ONLY if there is no error raised.

Note: Exceptions in the ELSE clause are NOT handled by the preceding EXCEPT clauses.

try:
x = 1/1
except:
print('Something went wrong')
else:
print('Nothing went wrong')

Here is an example that will divide the integer 1 by 1 - and store its result in the variable x. If an error occurs during this process, an exception is raised, then the except clause will catch and handle this error - printing out 'Something went wrong'. ELSE, it will print out 'Nothing went wrong' because the program did not run into any exceptions and the else clause was executed.

That is great! You can catch Exceptions, Handle them properly, and Execute some code when no exceptions are raised. But, How would you execute a block of code even though an error occurred? Let's find out throughout the next video...

Let's play this video, stick around and watch until the end of this video! 👍🏻

Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, Strings, Li...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assignmen...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF, ELIF...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Continu...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for Abso...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Sur cette page du site, vous pouvez voir la vidéo en ligne Exceptions in Python: Execute Code ONLY when NO Exception is Raised (TRY ELSE in Python) durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Digital Academy 28 avril 2021, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 533 fois et il a aimé 21 téléspectateurs. Bon visionnage!