Catch and Handle Exceptions in Python (TRY / EXCEPT in Python)

https://www.youtube.com/watch?v=ll0HYxzNm9g

🎓 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™ 🎓

***

☞ WATCH MORE:
○ Learn Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

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

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

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

#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/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

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

***

Copy link
Powered by Social Snap