{"record":{"id":"6dc0ff691f68fa6b","repo":"apple/pkl","slug":"divisionbyzero-6dc0ff","errorCode":"divisionByZero","errorMessage":"divisionByZero","messagePattern":"divisionByZero","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java","lineNumber":77,"sourceCode":"  public static long multiply(long x, long y) {\n    try {\n      return StrictMath.multiplyExact(x, y);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw intOverflow();\n    }\n  }\n\n  public static double multiply(double x, double y) {\n    return x * y;\n  }\n\n  public static long truncatingDivide(long x, long y) {\n    // for some reason, detecting division by zero by catching ArithmeticException\n    // does not work correctly in AOT mode, so let's do an explicit check for now\n    if (y == 0) {\n      CompilerDirectives.transferToInterpreter();\n      throw divisionByZero();\n    }\n\n    var result = x / y;\n\n    if ((x & y & result)\n        < 0) { // use same check as com.oracle.truffle.sl.nodes.expression.SLDivNode\n      CompilerDirectives.transferToInterpreter();\n      assert x == Long.MIN_VALUE && y == -1;\n      throw intOverflow();\n    }\n\n    return result;\n  }\n\n  public static long remainder(long x, long y) {\n    return x % y;\n  }\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java#L59-L95","documentation":"Pkl's truncating integer division (`~/`) throws `divisionByZero` when the divisor is 0. VmSafeMath.truncatingDivide performs an explicit zero check (rather than relying on catching ArithmeticException, which is unreliable in GraalVM AOT mode) and raises this eval error.","triggerScenarios":"Evaluating the `~/` Int division operator with a right-hand side that evaluates to 0 — commonly a computed denominator from config, a default `0` property, or an empty-collection-derived count.","commonSituations":"Computing ratios/averages where a totals property is 0 (e.g. percent = part * 100 ~/ total with total = 0); divisor read from external config/env that defaulted to 0.","solutions":["Guard the divisor: only divide when `y != 0`, else use a fallback value.","Fix the upstream value so the denominator is nonzero (correct the config or default).","For percentages/averages, special-case zero totals explicitly.","Use a null/absent marker for undefined ratios instead of dividing by a zero placeholder."],"exampleFix":"// before\npct = used * 100 ~/ total  // total may be 0\n\n// after\npct = if (total == 0) 0 else used * 100 ~/ total","handlingStrategy":"validation","validationCode":"// Pkl\nsafeDiv = (x: Int, y: Int) -> if (y == 0) 0 else x ~/ y","typeGuard":"function isNonZeroDivisor(y: Int): Boolean = y != 0","tryCatchPattern":null,"preventionTips":["Always guard `~/` with a zero check on the divisor","Validate externally supplied divisors before use","Special-case zero totals for ratios and averages","Avoid zero placeholders for 'unknown' values"],"tags":["pkl","arithmetic","division-by-zero"],"backgroundTag":"division-by-zero","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}