🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Catch and Handle Exceptions in Python (TRY / EXCEPT in Python)
In Python, ALL Exceptions can be handled using the TRY / EXCEPT statements. The critical operations which can raise an exception is placed inside the TRY clause. And the code that handles the exception is written in the EXCEPT clause, so you can choose what operations to perform once you have caught this exception. Let's have a closer look...
○ Catching ALL Exceptions in Python
When an error occurs: Python will throw an Exception, generate an error message and stop the program - if this exception is not handled properly. BUT, when this error occurs during its execution and inside the TRY statement, the rest of the try block is skipped as soon as this error is encountered - and the EXCEPT clause in Python is executed.
try:
statement_1
statement_fail
statement_not_executed
except:
print('Something went wrong!')
The very simple TRY and EXCEPT block in Python is used to catch and handle ALL these exceptions. Python will execute the code following the TRY statement, until an exception is encountered. The code that follows the EXCEPT statement determines how your program responds to ANY of these exceptions. Consequently, the EXCEPT clause is used to catch and handle ALL the exceptions in Python, which are encountered in the TRY clause.
What you did not get though, is the type of error that was thrown as result of the Exception! In order to see exactly what went wrong, you need to catch the error that the function threw, with an alias (except Exception AS error).
try:
statement_1
statement_fail
statement_not_executed
except Exception as error:
print('Something went wrong:', error)
Awesome, but what if you do not want to execute the same block of code, for every errors? Fortunately for you, Python provides the ability to catch a single exception, or even multiple exceptions at once. Let's find out ...
○ Catching Single Exception in Python
In the previous example, you did not mention any specific exception in the Except clause. It is not a good programming practice, as it will catch ALL exceptions regardless of your error - and handles every case in the same way. Nonetheless, you can specify which exceptions an except clause will catch: ValueError, TypeError, ZeroDivisionError, ImportError, etc.
try:
x = 1/0
except ZeroDivisionError:
print('Something went wrong!')
In above example, the try block will generate an exception, because a number is divided by zero. Therefore, the except block ZeroDivisionError will be executed ONLY for this specific error - any other errors will NOT be handled, and it may end up crashing your program.
○ Catching Multiple Exceptions in Python
A TRY clause can have any number of EXCEPT clauses, to handle different exceptions. BUT, ONLY one clause will be executed, if an exception occurs. Consequently, you can define as many EXCEPT blocks as you want, so that you can catch and handle specific exceptions, individually and differently. If you want to execute the same block of code, for multiple exceptions, specify ALL the exceptions inside a Tuple in Python.
try:
x = 1/0
except TypeError:
print('TypeError is raised')
except (ZeroDivisionError, ValueError):
print('ZeroDivisionError or ValueError is raised')
except:
print('Something else went wrong')
That is so cool. Your program will never crash anymore, since you can catch and handle ALL exceptions raised during its execution! BUT, How would you execute a block of code, ONLY when no exceptions are encountered in the TRY clause?
Let's play this video, stick around and watch until the end of this video! 👍🏻
Digital Academy™ 🎓
***
📖 Blog: http://digital.academy.free.fr/blog
📖 [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
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ / digital_academy
✧ https://www.buymeacoffee.com/digital_...
***
Watch video Catch and Handle Exceptions in Python (TRY EXCEPT in Python) - Python Tutorial for Beginners online without registration, duration hours minute second in high quality. This video was added by user Digital Academy 27 April 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 72 once and liked it 1 people.