{"record":{"id":"89e18c326ed20af5","repo":"TheAlgorithms/Java","slug":"array-must-be-non-empty-89e18c","errorCode":null,"errorMessage":"Array must be non-empty.","messagePattern":"Array must be non-empty\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FindMaxRecursion.java","lineNumber":17,"sourceCode":"package com.thealgorithms.maths;\n\npublic final class FindMaxRecursion {\n\n    private FindMaxRecursion() {\n    }\n    /**\n     * Get max of an array using divide and conquer algorithm\n     *\n     * @param array contains elements\n     * @param low the index of the first element\n     * @param high the index of the last element\n     * @return max of {@code array}\n     */\n    public static int max(final int[] array, final int low, final int high) {\n        if (array.length == 0) {\n            throw new IllegalArgumentException(\"Array must be non-empty.\");\n        }\n        if (low == high) {\n            return array[low]; // or array[high]\n        }\n\n        int mid = (low + high) >>> 1;\n\n        int leftMax = max(array, low, mid); // get max in [low, mid]\n        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]\n\n        return Math.max(leftMax, rightMax);\n    }\n\n    /**\n     * Get max of an array using recursion algorithm\n     *\n     * @param array contains elements\n     * @return max value of {@code array}","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java#L1-L35","documentation":"Thrown by FindMaxRecursion.max(int[] array, int low, int high) when array.length is 0. This divide-and-conquer method splits the range [low, high] into halves and recurses, terminating when low == high (returning array[low]). An empty array has no valid element to return. Note: the method checks array.length == 0 but does NOT validate that low/high are within bounds — passing an empty array is the only condition that triggers this specific message.","triggerScenarios":"Calling max(new int[0], 0, 0) or max(emptyArray, anyLow, anyHigh). The length check fires regardless of the low/high arguments. Also triggered if low/high are passed as values outside the array bounds without the length being zero (in that case you get a different ArrayIndexOutOfBoundsException, not this message).","commonSituations":"Passing empty arrays from filtered data. Mismatch between caller-supplied low/high indices and the actual array size. Recursive callers that shrink ranges without checking for emptiness.","solutions":["Ensure array.length > 0 before calling max().","Validate that 0 <= low <= high < array.length in addition to non-emptiness.","Wrap in a defensive helper that checks both the array and index bounds before delegating."],"exampleFix":"// before\nint result = FindMaxRecursion.max(arr, 0, arr.length - 1);\n\n// after\nif (arr.length == 0) {\n    throw new IllegalArgumentException(\"Cannot compute max of empty array\");\n}\nif (low < 0 || high >= arr.length || low > high) {\n    throw new IndexOutOfBoundsException(\"Invalid range [\" + low + \", \" + high + \"]\");\n}\nint result = FindMaxRecursion.max(arr, low, high);","handlingStrategy":"validation","validationCode":"if (array == null || array.length == 0) {\n    throw new IllegalArgumentException(\"Array must be non-empty\");\n}\nif (low < 0 || high >= array.length || low > high) {\n    throw new IndexOutOfBoundsException(\"Invalid range [\" + low + \", \" + high + \"]\");\n}\nint result = FindMaxRecursion.max(array, low, high);","typeGuard":"static boolean isValidRange(int[] array, int low, int high) {\n    return array != null && array.length > 0\n        && low >= 0 && high < array.length && low <= high;\n}","tryCatchPattern":"try {\n    int result = FindMaxRecursion.max(array, low, high);\n} catch (IllegalArgumentException e) {\n    // empty array; handle gracefully\n}","preventionTips":["Validate array non-emptiness AND index bounds before calling.","Ensure low <= high and both are within [0, length-1].","Check the array source for emptiness from filtering or partitioning."],"tags":["math","array","recursion","divide-and-conquer","empty-collection","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}