Use the finally block

EasyException Handling Programs · 8 of 20

Write a Python program that shows finally always runs.

Advertisement
Output
  result: 25.0
  clean-up done
  error handled
  clean-up done

Approach

  1. Run the function once successfully and once with an error.

Solution

Python
def process(value):    try:        print("  result:", 100 / value)    except ZeroDivisionError:        print("  error handled")    finally:        print("  clean-up done") process(4)process(0)
Output
  result: 25.0
  clean-up done
  error handled
  clean-up done

Learn the concept

New to this topic? Read the lesson, then come back.

More exception handling programs

← All 20 Exception Handling Programs

Leave a Comment