Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

try

The try statement runs potentially failing code and can respond with matching except handlers. Optional else handles success-only work, while finally provides cleanup that runs regardless of the outcome.

How it works

A try statement groups code that may raise an exception with handlers and optional success and cleanup clauses. Python executes the try suite first; a matching except suite handles the error, else runs only when the try suite succeeds, and finally runs for cleanup.

Syntax

try:
    suite
except [expression [as identifier]]:
    suite
else:
    suite
finally:
    suite

Key points

  • Use try to associate a statement group with exception handling and/or cleanup behavior.
  • A successful try suite skips every except handler.
  • Multiple except clauses can handle different exception types.
  • An exception with no matching handler propagates outward and eventually stops execution if it remains unhandled.
  • Use else for code that should run only after the protected operation succeeds.

Common mistakes

  • An exception raised inside one except handler is not handled by another handler in the same try statement.
  • Even when several except clauses could match through inheritance, only the first matching handler runs.
  • The else clause must come after every except clause.

Example: Handling conversion and cleanup

try:
    value = int("42")
except ValueError:
    value = 0
else:
    print(value)
finally:
    print("finished")

Parses the value, handles a conversion failure, runs the else block only after success, and always runs the finally block.

When to use it

Input validation

Parse or validate external input while converting expected failures into a controlled result.

Guaranteed cleanup

Release a file, lock, connection, or other resource after an operation completes or fails.

Success-only follow-up

Separate successful post-processing from the code whose failure is being handled.

Interview questions

QHow does Python choose an except handler?

The try suite runs first. If it raises an exception, Python searches the except clauses and executes the first matching handler; otherwise, handlers are skipped and execution continues after the statement.

Follow-up: What happens to an unmatched exception? It propagates outward to an enclosing try statement, or terminates execution as an unhandled exception if no handler matches.

QWhen does the else clause execute?

The else suite is optional and runs only when the try suite completes without raising an exception. It must appear after all except clauses.

Follow-up: Why place success-only code in else? It keeps that code outside the protected try suite, so exceptions it raises are not mistaken for failures of the attempted operation.

QWhat is finally used for?

The finally suite is used for cleanup and runs regardless of whether the try suite succeeds or an exception is handled. A try statement may also use finally without except or else.

Follow-up: What if an exception remains unhandled? Python still executes finally before propagating the exception or reporting it as unhandled.

  • except
  • else
  • finally
  • BaseException
  • Exception

Source: 8. Compound statements — Python 3.16.0a0 documentation. Checked 2026-10-16.