Python Try...Except

Python · Tooling & Errors

Theory

When something goes wrong at runtime - dividing by zero, a missing file, bad user input - Python raises an exception, and if nothing handles it the program stops with a traceback. A try/except statement lets you catch it. Code that might fail goes inside try; if an exception occurs, Python stops that block immediately and jumps to the first except whose type matches, such as except ZeroDivisionError.

The full statement has four parts: try (risky code), except (runs on a matching error), else (runs only when no error happened) and finally (always runs, used for cleanup). except ValueError as e gives you the exception object so you can log its message. Catch the specific errors you expect; a bare except: also swallows bugs and even Ctrl+C, hiding real problems.

Why we use it

Programs meet bad data and failing resources all the time. Exception handling lets them respond sensibly - show a friendly message, skip a bad row, retry - instead of crashing halfway through a job.

When to use it

Use try/except around operations that can fail for reasons outside your control: input parsing, file and network access, type conversions. Do not use it to hide bugs; fix logic errors instead of catching them.

How it works

  1. Enter the try block
  2. Run the risky code
  3. Did an exception occur? Yes: Jump to the matching except block. No: Run the else block (if any).
  4. Run the finally block (always)
  5. Program continues instead of crashing

Syntax

try:
    ...
except ExceptionType:
    ...

Example

try:
    result = 10 / 0
except ZeroDivisionError:
    print('Cannot divide by zero')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic