{"record":{"id":"10857d83e6d7796e","repo":"TheAlgorithms/Java","slug":"capacity-must-greater-than-0","errorCode":null,"errorMessage":"capacity must greater than 0!","messagePattern":"capacity must greater than 0!","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java","lineNumber":95,"sourceCode":"    private Entry<K, V> evict() {\n        if (head == null) {\n            throw new RuntimeException(\"cache cannot be empty!\");\n        }\n        Entry<K, V> evicted = head;\n        head = evicted.getNextEntry();\n        head.setPreEntry(null);\n        evicted.setNextEntry(null);\n        return evicted;\n    }\n\n    /**\n     * Checks if the capacity is valid.\n     *\n     * @param capacity the capacity to check\n     */\n    private void checkCapacity(int capacity) {\n        if (capacity <= 0) {\n            throw new RuntimeException(\"capacity must greater than 0!\");\n        }\n    }\n\n    /**\n     * Returns the value to which the specified key is mapped, or null if this cache contains no\n     * mapping for the key.\n     *\n     * @param key the key whose associated value is to be returned\n     * @return the value to which the specified key is mapped, or null if this cache contains no\n     * mapping for the key\n     */\n    public V get(K key) {\n        if (!data.containsKey(key)) {\n            return null;\n        }\n        final Entry<K, V> entry = data.get(key);\n        moveNodeToLast(entry);\n        return entry.getValue();","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java#L77-L113","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the capacity value is >= 1 before constructing the cache","Validate configuration at application startup and fail fast with a descriptive message","Fall back to a sensible default (e.g., 100) when the configured value is invalid"],"exampleFix":"// before\nint cap = config.getCacheSize(); // may be 0\nLRUCache<String,String> cache = new LRUCache<>(cap);\n\n// after\nint cap = config.getCacheSize();\nif (cap < 1) throw new IllegalStateException(\"cache size must be >= 1, got \" + cap);\nLRUCache<String,String> cache = new LRUCache<>(cap);","handlingStrategy":"validation","validationCode":"int cap = config.getCacheCapacity();\nif (cap < 1) {\n    throw new IllegalStateException(\"LRUCache capacity must be >= 1, got: \" + cap);\n}\nLRUCache<String,String> cache = new LRUCache<>(cap);","typeGuard":null,"tryCatchPattern":"try {\n    cache = new LRUCache<>(cap);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"capacity must greater than 0\")) {\n        logger.error(\"Invalid cache capacity: \" + cap);\n        cache = new LRUCache<>(); // fall back to default 100\n    } else throw e;\n}","preventionTips":["Validate all externally-sourced capacity values before construction","Use constants or configuration validators for cache sizing","Prefer the no-arg constructor when the exact size does not matter"],"tags":["lru-cache","configuration","validation","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}