AI engineering glossary · Python core

What is try/except?

try/except (also called exception handling): A construct that runs a block of code and, if it raises an exception, jumps to a matching except block to handle it instead of crashing. You can name the exception types to catch and add an else block for the no-error case.

Level: beginnerTopic: Python core

try/except: an example

try: n = int(text) except ValueError: n = 0 sets n to 0 when text is 'abc'.

Related terms

More Python core terms

Where you learn it

try/except is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.