← All examples

Safe division

Python Exceptions

try / except

Flowchart (ISO 5807)

StartInput a, bReturn a / bEndFigure 1 — safe_div

Source code

def safe_div(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return 0