Else (Try)

Python · Exception Handling

Theory

A try statement can have an else block after its except blocks. The else block runs only when the try block finished without raising any exception. If an exception was raised and caught, else is skipped entirely.

In the example, 10 / 2 succeeds, so the except is skipped and else prints 'Success: 5.0'. If the division were 10 / 0, except would print 'Error' and else would not run. Why not put that print inside try? Because code in else is not protected by the except clauses: if it raised its own ZeroDivisionError, that would not be mistaken for the error you meant to catch. Using else keeps the try block focused on the one risky operation and makes the success path explicit.

Why we use it

It separates 'the risky step' from 'what to do once it worked'. This keeps try blocks small and stops your except clauses from accidentally catching errors raised by the follow-up code.

When to use it

Use else when some code should run only if the risky step succeeded, such as processing data after it loaded. If the follow-up is a single trivial line, many developers simply place it after the whole try statement.

How it works

  1. Run the try block
  2. Did an exception occur? Yes: Run the matching except block. No: Run the else block.
  3. Run finally if present
  4. Continue after the statement

Syntax

try:
    ...
except Error:
    ...
else:
    ...

Example

try:
    result = 10 / 2
except ZeroDivisionError:
    print("Error")
else:
    print(f"Success: {result}")

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