{"record":{"id":"81c4e4ba055f47ba","repo":"Shopify/liquid","slug":"e-message-re-raised-as-liquid-zerodivisionerror","errorCode":null,"errorMessage":"e.message (re-raised as Liquid::ZeroDivisionError from ::ZeroDivisionError in divided_by)","messagePattern":"e\\.message \\(re-raised as Liquid::ZeroDivisionError from ::ZeroDivisionError in divided_by\\)","errorType":"exception","errorClass":"Liquid::ZeroDivisionError","httpStatus":null,"severity":"error","filePath":"lib/liquid/standardfilters.rb","lineNumber":866,"sourceCode":"    # @liquid_summary\n    #   Multiplies a number by a given number.\n    # @liquid_syntax number | times: number\n    # @liquid_return [number]\n    def times(input, operand)\n      apply_operation(input, operand, :*)\n    end\n\n    # @liquid_public_docs\n    # @liquid_type filter\n    # @liquid_category math\n    # @liquid_summary\n    #   Divides a number by a given number. The `divided_by` filter produces a result of the same type as the divisor. This means if you divide by an integer, the result will be an integer, and if you divide by a float, the result will be a float.\n    # @liquid_syntax number | divided_by: number\n    # @liquid_return [number]\n    def divided_by(input, operand)\n      apply_operation(input, operand, :/)\n    rescue ::ZeroDivisionError => e\n      raise Liquid::ZeroDivisionError, e.message\n    end\n\n    # @liquid_public_docs\n    # @liquid_type filter\n    # @liquid_category math\n    # @liquid_summary\n    #   Returns the remainder of dividing a number by a given number.\n    # @liquid_syntax number | modulo: number\n    # @liquid_return [number]\n    def modulo(input, operand)\n      apply_operation(input, operand, :%)\n    rescue ::ZeroDivisionError => e\n      raise Liquid::ZeroDivisionError, e.message\n    end\n\n    # @liquid_public_docs\n    # @liquid_type filter\n    # @liquid_category math","sourceCodeStart":848,"sourceCodeEnd":884,"githubUrl":"https://github.com/Shopify/liquid/blob/807d45a6b3d4568e64e86b375e3702df2c7c860c/lib/liquid/standardfilters.rb#L848-L884","documentation":"The `divided_by` filter wraps Ruby's native ::ZeroDivisionError and re-raises it as Liquid::ZeroDivisionError. This happens when the divisor operand evaluates to zero (or 0.0 for float division producing Infinity handling in some cases). Liquid converts the low-level Ruby error so templates surface a consistent error type.","triggerScenarios":"Calling a template filter like `{{ 10 | divided_by: 0 }}` or `{{ price | divided_by: divisor }}` where `divisor` is 0 or evaluates to 0 after Utils.to_number coercion.","commonSituations":"Rendering templates with computed divisors (counts, averages, percentages) where the denominator variable is empty, 0, or nil-coerced to 0; e.g. average-per-item calculations over empty collections.","solutions":["Guard the divisor in the template: use `{% if divisor != 0 %}` before applying divided_by.","Default the operand with the `default` filter: `{{ total | divided_by: count | default: 0 }}` only helps if count is nil, so validate count != 0 first.","Fix the data source so the denominator is never 0 (e.g. skip averaging when items list is empty).","Rescue Liquid::ZeroDivisionError in your render error handling and render a fallback message.","Ensure operand is coerced correctly (strings like \"0\" also trigger it; check the value bound to the variable)."],"exampleFix":"// before\n{% assign avg = total | divided_by: count %}\n// after\n{% if count > 0 %}{% assign avg = total | divided_by: count %}{% else %}{% assign avg = 0 %}{% endif %}","handlingStrategy":"try-catch","validationCode":"{% if divisor and divisor != 0 %}{{ total | divided_by: divisor }}{% else %}0{% endif %}","typeGuard":"def zero?(v)\n  v.respond_to?(:to_f) && v.to_f == 0.0\nend","tryCatchPattern":"begin\n  template.render(assigns)\nrescue Liquid::ZeroDivisionError => e\n  fallback_division_output\nend","preventionTips":["Always validate denominators are non-zero before binding them to template assigns","Use the `default` filter for nil operands but still check for literal 0","Add render-level error handling for Liquid::ZeroDivisionError in production","Unit-test templates with zero/empty denominator edge cases"],"tags":["liquid","math","division-by-zero","template"],"backgroundTag":"invalid-argument-value","analyzedSha":"807d45a6b3d4568e64e86b375e3702df2c7c860c","analyzedAt":"2026-09-08T11:31:38.917Z","contentChangedAt":"2026-09-08T11:31:38.917Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}