---
title: "Exceptions: Raising, Catching and Recovering"
description: "Signalling an unusable input or failed operation with an exception, then choosing a deliberate handling policy. Expected failures may be recovered from; other failures may be reported and propagated r"
canonical: https://lightmysky.com/learn/computing/exceptions-raising-catching-and-recovering-mt_hEVa1JRrGu
source: https://lightmysky.com/learn/computing/exceptions-raising-catching-and-recovering-mt_hEVa1JRrGu.md
retrieved: 2026-11-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Exceptions: Raising, Catching and Recovering

Signalling an unusable input or failed operation with an exception, then choosing a deliberate handling policy. Expected failures may be recovered from; other failures may be reported and propagated rather than silently suppressed.

Subject: Computing · Area: Programming · Ages 17 to 18
Page: https://lightmysky.com/learn/computing/exceptions-raising-catching-and-recovering-mt_hEVa1JRrGu

## Ready when they can

- Catch a specific exception around a risky operation and recover from it
- Raise an exception when a function is given input it cannot work with
- Explain why catching every exception and continuing is usually the wrong repair

## Lesson: Raising and catching failures

Exception types depend on the operation. open('notes.txt', 'r') raises FileNotFoundError if the path is absent. int('leaf') raises ValueError because the text is not an integer. Accessing an out-of-range list position raises IndexError. Opening in 'w' or 'a' can create a missing file instead.

**Example.** try runs the risky operation. A matching except handles its exception; later lines inside that try are skipped.
```python
try:
    number = int("leaf")
    print(number)
except ValueError:
    print("Use integer digits")
```
The conversion raises ValueError, so the handler prints its message instead of printing number.

*(drawing: Use the converted number only after conversion succeeds. A message alone does not produce it.)*

**Example.** A function can reject input explicitly:
```python
def half_positive(n):
    if n <= 0:
        raise ValueError("positive required")
    return n / 2
```
raise interrupts this call. The caller may handle the exception; the function does not return a made-up numeric result.

Choose a handler from the operation and its failure conditions. Opening an absent path in 'r' raises FileNotFoundError; converting nonnumeric text with int raises ValueError. A missing configuration file might be reported before stopping safely; invalid numeric text might prompt another input request. Other read or conversion failures can have different types. Use a result only after its operation succeeds.

**Example.** Bare except catches broadly; pass does nothing. Silent continuation can reuse stale data:
```python
number = 5
try:
    number = int("leaf")
except:
    pass
print(number)
```
The failed assignment leaves 5 unchanged. A deliberate policy must decide whether continuation is valid.

**Example.** Broad handling need not suppress a failure:
```python
try:
    operation()
except Exception as err:
    print(err)
    raise
```
Bare raise propagates the current exception after reporting it. A handler may recover, report and propagate, or stop safely. In a plain script with default reporting, an uncaught exception prints an error and stops the script.

**Recap.** Match the exception to the failed operation. Raise for unusable input. Handle with a deliberate policy: recover, report and propagate, or stop safely.

*Exception teaching adapted from the Introduction to Python Programming chapter 14 corpus records, with independently written control-flow examples, handling-policy explanations and assessment repairs.*

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Reading and Writing Files, Including CSV](https://lightmysky.com/learn/computing/reading-and-writing-files-including-csv-mt_j5qb4FdkdI)

## Opens up

- [Unit Tests: Deciding What Is Worth Testing](https://lightmysky.com/learn/computing/unit-tests-deciding-what-is-worth-testing-mt_tuIQecq5Le)
