Except

Python · Exception Handling

Theory

An except clause says which exception types to handle and what to do. except ValueError: catches only ValueError (and its subclasses); except (ValueError, TypeError): catches either; except ValueError as e: also binds the exception object to e, so print(e) shows its message, such as "invalid literal for int() with base 10: 'abc'".

You can stack several except blocks after one try. Python checks them top to bottom and runs only the first one that matches, so put specific exceptions before general ones like Exception. If none match, the exception continues up to the caller. A bare except: catches everything, including KeyboardInterrupt and SystemExit, so it can hide real bugs; if you must catch broadly, use except Exception as e and log the error.

Why we use it

Different failures need different responses: bad input might be re-asked, a missing file created, a network error retried. except lets you match each error type to its own recovery.

When to use it

Catch the specific exceptions you can actually handle. Let unexpected exceptions propagate (or log and re-raise them with raise) so bugs stay visible instead of being silently swallowed.

How it works

  1. An exception is raised in try
  2. Check the first except clause
  3. Does the exception type match? Yes: Run this except block. No: Check the next except clause.
  4. Any except clauses left? Yes: Repeat the type check. No: Exception propagates to the caller.
  5. Program continues after the try statement

Syntax

except ExceptionType as e:
    handle(e)

Example

try:
    x = int('abc')
except ValueError as e:
    print(f'Invalid input: {e}')

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