HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Exceptions Lesson

Python Try Except

5 min to complete · By Martin Breuss

When Python encounters a line of code that it can't handle during execution, it will raise an exception. You've probably seen a lot of them already.

Handling Exceptions

Unless the exception is handled, your application will quit and the exception message will appear in your terminal. While this feedback from your Python interpreter is extremely helpful for you as a developer, you don't want to show such exception messages to your users.

Your users might not yet know that errors are their friends, and they might not like how Python tells them that something has gone wrong. Most of all, however, the exception will quit your program, and your users won't have the option to do anything about that. To avoid confusion, you can implement try except blocks in your code as an error handling technique.

How to Use Python Try Except

You use a try except block when you want to try a piece of code that might raise an exception. The second part is the except statement that handles the situation when any exception is raised.

Code Example

In the code block below, you can see the basic structure of a try except block, with pass keywords as placeholders for the code you'll implement:

try:
    pass  # The code to try
except:
    pass  # What to do if an exception occurred

This represents the basic structure of a try except block. Applied to a functioning example, it could look like this:

try:
    num = int(input("Enter a number: "))
except:
    print("You didn't enter a number!")

Code Explanation

In this code example, you ask your users to input a number in their CLI. Inside the try block, you try to convert their input to an integer. If the user enters a number that can be successfully converted, no exception will be raised, and your code skips the except block and continues execution below it.

However, if the user enters a word instead of a number, the int() type conversion will fail. It will do so by raising a built-in exception called ValueError. This is a Python exception, which is why your code will jump to the except block and run the indented code in there. This will print out a message to the user before continuing execution below the try except block.

Colorful illustration of a light bulb

Info: If you catch an exception with except, your program won't quit and will continue to run any additional lines of code that follow the try/except block.

Using an except keyword like in the example above will catch any exception that got raised in the try block. If you want to be more specific, you can declare what kind of exception should be handled by the except block.

While using a plain except without any further specification might sound like the least effort for you, it's good practice to be as precise as possible with your exception handling. If possible, you'll want to avoid using catch-alls. In the next lesson, you'll learn how to handle specific exceptions.

Summary: Python Try Except

  • Exceptions are when your program finds a line of code that it can't handle
  • If an exception is found and not handled, then your program will quit
  • The try except block will try a piece of code in the try block and run the except block if an error arises
  • The except keyword will catch all types of errors