Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Errors, files and libraries · Lesson 17 of 25

    Handling exceptions

    try, except, else and finally without hiding errors.

    8 minutes2 games

    An exception interrupts normal execution. Put the operation that may fail in try and catch the expected type with except. else runs when try succeeds; finally handles cleanup whether an error occurred or not.

    Python
    raw = "twelve"
    try:
        age = int(raw)
    except ValueError:
        print("Enter a whole number")
    else:
        print(age + 1)
    finally:
        print("Done")

    Try conversion

    Success → else

    ValueError → except

    Cleanup → finally

    Important

    Do not silently swallow every exception. Catch ValueError for invalid conversion; unexpected errors should remain visible during development.

    Interview question

    How do SyntaxError and ValueError differ?

    Spot the bug

    Which line raises ValueError?

    Exercise

    Print Invalid if conversion fails.

    Expected output: Invalid

    Python
    raw = "oops"
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousComprehensions, enumerate and zipNextModules and the standard library
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.