← All examples

Safe division

PHP Exceptions

try / catch

Flowchart (ISO 5807)

YesNoStartInput a, bb == 0Error: ExceptionEndReturn a / bEndFigure 1 — safeDiv

Source code

function safeDiv($a, $b) {
    try {
        if ($b == 0) throw new Exception("нуль");
        return $a / $b;
    } catch (Exception $e) {
        return 0;
    }
}