clojure/clojure · error · IllegalArgumentException
Value out of range for short:
Error message
Value out of range for short:
What it means
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.
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.
Example fix
// before
(short (:count m)) ; throws for 100000
// after
(let [n (long (:count m))]
(if (<= Short/MIN_VALUE n Short/MAX_VALUE)
(short n)
(throw (ex-info "count exceeds short range" {:count n})))) Defensive patterns
Strategy: validation
Validate before calling
(defn short-safe? [x]
(try (let [n (long x)] (<= Short/MIN_VALUE n Short/MAX_VALUE))
(catch Exception _ false))) Type guard
(defn ->short [x] (when (short-safe? x) (short x)))
Try / catch
(try
(short x)
(catch IllegalArgumentException _
(handle-out-of-range x))) Prevention
- 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
When it happens
Trigger: Calling (short x) / RT.shortCast(Object) with a Long, Integer, BigInt, BigInteger, Ratio or other Number whose longCast value is < -32768 or > 32767.
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Value out of range for float:
- Value out of range for int:
- Value out of range for long:
- AbortException
- Accessor/struct mismatch
AI-assisted analysis of clojure/clojure@f3b143341d (2026-09-09).
Data as JSON: /api/errors/2a8ed3c0cc4ab96a.
Report an issue: GitHub.
Appendix: source
Thrown at src/jvm/clojure/lang/RT.java:1168
static public byte byteCast(float x){
if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
return (byte) x;
throw new IllegalArgumentException("Value out of range for byte: " + x);
}
static public byte byteCast(double x){
if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
return (byte) x;
throw new IllegalArgumentException("Value out of range for byte: " + x);
}
static public short shortCast(Object x){
if(x instanceof Short)
return ((Short) x).shortValue();
long n = longCast(x);
if(n < Short.MIN_VALUE || n > Short.MAX_VALUE)
throw new IllegalArgumentException("Value out of range for short: " + x);
return (short) n;
}
static public short shortCast(byte x){
return x;
}
static public short shortCast(short x){
return x;
}
static public short shortCast(int x){
short i = (short) x;
if(i != x)
throw new IllegalArgumentException("Value out of range for short: " + x);
return i;
}View on GitHub (pinned to f3b143341d)