Python Practice › Exception Handling Programs › Program 8
Use the finally block
Write a Python program that shows finally always runs.
Output
result: 25.0 clean-up done error handled clean-up done
Approach
- Run the function once successfully and once with an error.
Solution
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.