TheAlgorithms/Java · error · RuntimeException

capacity must greater than 0!

Error message

capacity must greater than 0!

What it means

Thrown by the private checkCapacity() guard when an LRUCache is constructed (or internally resized) with a capacity of zero or less. The cache needs at least one slot to hold entries. Note: this uses a generic RuntimeException rather than IllegalArgumentException, which is inconsistent with the MRUCache counterpart.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java:95

    private Entry<K, V> evict() {
        if (head == null) {
            throw new RuntimeException("cache cannot be empty!");
        }
        Entry<K, V> evicted = head;
        head = evicted.getNextEntry();
        head.setPreEntry(null);
        evicted.setNextEntry(null);
        return evicted;
    }

    /**
     * Checks if the capacity is valid.
     *
     * @param capacity the capacity to check
     */
    private void checkCapacity(int capacity) {
        if (capacity <= 0) {
            throw new RuntimeException("capacity must greater than 0!");
        }
    }

    /**
     * Returns the value to which the specified key is mapped, or null if this cache contains no
     * mapping for the key.
     *
     * @param key the key whose associated value is to be returned
     * @return the value to which the specified key is mapped, or null if this cache contains no
     * mapping for the key
     */
    public V get(K key) {
        if (!data.containsKey(key)) {
            return null;
        }
        final Entry<K, V> entry = data.get(key);
        moveNodeToLast(entry);
        return entry.getValue();

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the capacity value is >= 1 before constructing the cache
  2. Validate configuration at application startup and fail fast with a descriptive message
  3. Fall back to a sensible default (e.g., 100) when the configured value is invalid

Example fix

// before
int cap = config.getCacheSize(); // may be 0
LRUCache<String,String> cache = new LRUCache<>(cap);

// after
int cap = config.getCacheSize();
if (cap < 1) throw new IllegalStateException("cache size must be >= 1, got " + cap);
LRUCache<String,String> cache = new LRUCache<>(cap);
Defensive patterns

Strategy: validation

Validate before calling

int cap = config.getCacheCapacity();
if (cap < 1) {
    throw new IllegalStateException("LRUCache capacity must be >= 1, got: " + cap);
}
LRUCache<String,String> cache = new LRUCache<>(cap);

Try / catch

try {
    cache = new LRUCache<>(cap);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().contains("capacity must greater than 0")) {
        logger.error("Invalid cache capacity: " + cap);
        cache = new LRUCache<>(); // fall back to default 100
    } else throw e;
}

Prevention

When it happens

Trigger: Calling new LRUCache(0), new LRUCache(-1), or passing any int <= 0 to the LRUCache(int cap) constructor. The no-arg constructor uses a default of 100 and is safe.

Common situations: Capacity is read from a config file, environment variable, or database that defaults to 0. Off-by-one when deriving capacity from a collection size (e.g., size - 1). Loading capacity from unvalidated user input.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/10857d83e6d7796e. Report an issue: GitHub.