Try, Except

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..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

profile avatar
felixt(@felixt)
1 year ago

Great content

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete