Handling Exceptions
Learn how to handle exceptions in Python.
try..except
block
Let’s look at the tail side of the exception coin. If we encounter an exception situation, how should our code react to or recover from it? We handle exceptions by wrapping any code that might throw one (whether it is exception code itself, or a call to any function that may have an exception raised) inside a try...except
clause.
Handle an exception
If we run this simple script below using our existing never_returns()
function—which, as we know very well, always throws an exception—we get the following output:
Get hands-on with 1200+ tech skills courses.