Try

Python · Exception Handling

Theory

The try block marks code that might raise an exception. Python runs it line by line as usual; if every line succeeds, the except blocks are skipped. The moment one line raises an exception, the rest of the try block is skipped and Python looks for an except clause whose type matches the error.

In the example, 10 / 0 raises ZeroDivisionError, so result is never assigned and control jumps to except ZeroDivisionError, which prints a message. A try must be followed by at least one except or a finally. Only exceptions raised inside the try block (including inside functions it calls) are handled; an error raised inside an except or finally block is not caught by the same statement. Keep the try block small so you catch only the failure you expect.

Why we use it

It marks exactly which code is risky, so the program can recover from a predictable failure - a bad value, a missing file - instead of stopping with a traceback.

When to use it

Wrap the specific lines that depend on outside conditions: conversions, file and network calls, division by user-provided numbers. Do not wrap large blocks 'just in case'; that hides bugs and makes errors hard to trace.

How it works

  1. Enter the try block
  2. Run the next line
  3. Did this line raise? Yes: Skip rest of try, find matching except. No: Continue to the next line.
  4. Matching except found? Yes: Run that except block. No: Exception propagates to the caller.
  5. Continue after the try statement

Syntax

try:
    risky_code()

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