{"record":{"id":"2a8ed3c0cc4ab96a","repo":"clojure/clojure","slug":"value-out-of-range-for-short","errorCode":null,"errorMessage":"Value out of range for short: ","messagePattern":"Value out of range for short: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/jvm/clojure/lang/RT.java","lineNumber":1168,"sourceCode":"\nstatic public byte byteCast(float x){\n    if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)\n        return (byte) x;\n    throw new IllegalArgumentException(\"Value out of range for byte: \" + x);\n}\n\nstatic public byte byteCast(double x){\n    if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)\n        return (byte) x;\n    throw new IllegalArgumentException(\"Value out of range for byte: \" + x);\n}\n\nstatic public short shortCast(Object x){\n\tif(x instanceof Short)\n\t\treturn ((Short) x).shortValue();\n\tlong n = longCast(x);\n\tif(n < Short.MIN_VALUE || n > Short.MAX_VALUE)\n\t\tthrow new IllegalArgumentException(\"Value out of range for short: \" + x);\n\n\treturn (short) n;\n}\n\nstatic public short shortCast(byte x){\n\treturn x;\n}\n\nstatic public short shortCast(short x){\n\treturn x;\n}\n\nstatic public short shortCast(int x){\n    short i = (short) x;\n    if(i != x)\n        throw new IllegalArgumentException(\"Value out of range for short: \" + x);\n    return i;\n}","sourceCodeStart":1150,"sourceCodeEnd":1186,"githubUrl":"https://github.com/clojure/clojure/blob/f3b143341d6efc6428b523b2eaa099a6cc99156e/src/jvm/clojure/lang/RT.java#L1150-L1186","documentation":"Clojure's RT.shortCast(Object) narrows any numeric value to a primitive short via longCast. If the value falls outside Short.MIN_VALUE (-32768)..Short.MAX_VALUE (32767), the cast is refused with IllegalArgumentException instead of silently truncating. It is thrown eagerly to preserve numeric fidelity when coercion (e.g. via (short x)) is requested.","triggerScenarios":"Calling (short x) / RT.shortCast(Object) with a Long, Integer, BigInt, BigInteger, Ratio or other Number whose longCast value is < -32768 or > 32767.","commonSituations":"Narrowing user-supplied or computed numbers into short fields (interop with Java APIs, byte-buffer packing, DB smallint columns); big results of arithmetic like (* 1000 1000) cast to short; parsing large numbers then coercing down.","solutions":["Range-check the value before casting: (when (<= Short/MIN_VALUE n Short/MAX_VALUE) (short n)).","Use a wider type (int/long) if the magnitude is inherent to the data.","Clamp or wrap explicitly with bit-and 0xFFFF if truncation is actually intended.","Fix upstream parsing/construction that produced an unexpectedly large value."],"exampleFix":"// before\n(short (:count m)) ; throws for 100000\n// after\n(let [n (long (:count m))]\n  (if (<= Short/MIN_VALUE n Short/MAX_VALUE)\n    (short n)\n    (throw (ex-info \"count exceeds short range\" {:count n}))))","handlingStrategy":"validation","validationCode":"(defn short-safe? [x]\n  (try (let [n (long x)] (<= Short/MIN_VALUE n Short/MAX_VALUE))\n    (catch Exception _ false)))","typeGuard":"(defn ->short [x]\n  (when (short-safe? x) (short x)))","tryCatchPattern":"(try\n  (short x)\n  (catch IllegalArgumentException _\n    (handle-out-of-range x)))","preventionTips":["Range-check any externally sourced number before narrowing to short","Prefer int/long types unless short is required by an external API","Use unchecked-short only with a comment justifying truncation","Add :pre conditions or spec on coercion boundaries"],"tags":["clojure","numeric-cast","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"f3b143341d6efc6428b523b2eaa099a6cc99156e","analyzedAt":"2026-09-09T12:04:58.961Z","contentChangedAt":"2026-09-09T12:04:58.961Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}