Try, Except
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
Hello there! In this lesson, we're going to learn about the try, except, else, and finally keywords in Python.
The try keyword is used to define a block of code that will be executed and potentially raise an error. If an error is raised, the code inside the except block will be executed to handle the error. Here's an example of how try and except can be used in Python:
try:
# Some code that might raise an error
1 / 0
except ZeroDivisionError:
# Code to handle the error
print("You cannot divide by zero!")
In this code, we use the try keyword to define a block of code that attempts to divide 1 by 0. Since this operation is not allowed, it will raise a ZeroDivisionError.
We use the except keyword to define a block of code that will be executed if a ZeroDivisionError is raised. In this case, the code inside the except block prints the message "You cannot divide by zero!" to the screen.
The else keyword can be used in conjunction with try and except to define a block of code that will be executed only if no errors are raised in the try block. Here's an example that uses try, except, and else:
try:
# Some code that might raise an error
1 / 1
except ZeroDivisionError:
# Code to handle the error
print("You cannot divide by zero!")
else:
# Code to be executed if no errors are raised
print("The operation was successful!")
In this code, we use the try keyword to define a block of code that divides 1 by 1. Since this operation is allowed, no errors are raised, and the code inside the else block is executed. In this case, the code inside the else block prints the message "The operation was successful!" to the screen.
Finally, the finally keyword can be used in conjunction with try and except to define a block of code that will be executed regardless of whether an error is raised in the try block. Here's an example that uses try, except, and finally:
try:
# Some code that might raise an error
1 / 0
except ZeroDivisionError:
# Code to handle the error
print("You cannot divide by zero!")
finally:
# Code to be executed regardless of whether an error is raised
print("This code will always be executed")
In Python, you can use multiple except statements to handle different types of errors that might be raised in the try block. Here's an example of how to handle multiple exceptions:
try:
# Some code that might raise an error
1 / 0
except ZeroDivisionError:
# Code to handle the ZeroDivisionError
print("You cannot divide by zero!")
except TypeError:
# Code to handle the TypeError
print("You cannot perform this operation on these types!")
In this code, we use the try keyword to define a block of code that attempts to divide 1 by 0. Since this operation is not allowed, it will raise a ZeroDivisionError.
We use the except keyword to define two blocks of code that will be executed if a ZeroDivisionError or TypeError is raised. In this example, the code inside the except block that handles the ZeroDivisionError prints the message "You cannot divide by zero!" to the screen.
You can also use the else and finally keywords in conjunction with multiple except statements, just like in the examples above.
I hope this helps you understand how to use try, except, else, and finally to handle errors in Python. See you in the next lesson!
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 15min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 7 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 7 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 36min
0 / 8 lessons complete
Section Overview
Text | 3 min
Casting
Text | 3 min
Strings
Text | 4 min
Booleans
Text | 7 min
User Input
Text | 2 min
Numbers
Text | 6 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 38min
0 / 6 lessons complete
Python Lists
Text | 7 min
Python Tuples
Text | 7 min
Python Sets
Text | 6 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 4 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 29min
0 / 5 lessons complete
Python Loops • 20min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 4 min
While Loops
Text | 4 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 7 min
Python PIP and Modules • 15min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 3 min
Installing Modules with PIP
Text | 4 min
Importing Modules
Text | 5 min
RegEx • 23min
0 / 4 lessons complete
Section Overview
Text | 3 min
Regex 101
Text | 9 min
Importing Regex and manipulating strings
Text | 6 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete

Great content
Sound that “Except TypeError” doesn’t work to handle the type error.
Hi
André Serville
Why do you say that? did it gave you an error on some operation your were doing?
The use of the try with two excepts like ZeroDivisionError and TypeError will return just the ZeroDivisionError.
I expected to have two returns: one from ZeroDivisionError and the other one from TypeError.
Finally, as only ZeroDivisionError succeds, I comment it to execute only TypeError, but the code raises an error.
That is the why of my comment.
Perhaps due to my understanding, i miss to fix anything, i don’t know.
Thank you!
Oh! It’s that Python stops at the first exception and doesn’t continue to run the rest of the code.