Skip to content

Safe division

JavaScript try / catch

Same task in: PythonGoPHPRust

Flowchart (ISO 5807)

YesNoStartInput a, bb = 0Error: «нуль»EndReturn a / bEndFigure 1 — safeDiv

Source code

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

The editor opens with this code — change it and the chart redraws.

Language
JavaScript
Constructs
try / catch
Topic
Exceptions
Formats
Word, Visio, draw.io, PDF, SVG

Related examples