← All Exercises

Python Exception Handling

basicpython~15min

Background

Exception handling is critical for robust programs, preventing crashes and providing meaningful error messages.

Problem

Write a function that safely divides two numbers, handling division by zero and type errors with appropriate error messages.

Input

Two values to divide (numerator and denominator).

Output

The division result or an error message string.

Sample tests

Sample 1

Input: 10, 2
Expected: 5.0

Your solution

Learning objective

Learn structured exception handling with multiple except clauses and else/finally blocks.