Safe Division
Beginner · Write the code · from Python Try-Except: Handle Errors Without Crashing
From the lesson: Python Try-Except: Handle Errors Without Crashing
Imagine a trapeze artist performing high above the ground. No matter how skilled they are, there's always a safety net below. If they slip, the net catches them and they can climb back up and try again.
Read the lessonWrite a function called safe_divide(a, b) that returns the result of a / b. If b is zero, it should catch the ZeroDivisionError and return the string "Cannot divide by zero" instead.
Print the result of calling safe_divide(10, 2) and then safe_divide(10, 0).
# Write your safe_divide function here
More Try-Except exercises
- Predict the OutputBeginner
- Fix the Number ConverterIntermediate
- Input Validator with raiseIntermediate
- Safe Dictionary LookupAdvanced
- Build a Safe CalculatorAdvanced
All Python exercises · 50 practice problems · Error Handling quiz