TheAlgorithms/Java · error · IllegalArgumentException

array must be non-empty.

Error message

array must be non-empty.

What it means

Thrown by FindMinRecursion.min(int[] array, int low, int high) when array.length is 0. This is the recursive divide-and-conquer counterpart to FindMinRecursion, splitting [low, high] recursively and returning array[low] when low == high. An empty array yields no valid minimum. Note the message uses lowercase 'array' unlike FindMax's 'Array', so string-matching on the exact case matters if catching by message.

Source

Thrown at src/main/java/com/thealgorithms/maths/FindMinRecursion.java:19

package com.thealgorithms.maths;

public final class FindMinRecursion {

    private FindMinRecursion() {
    }

    /**
     * Get min of an array using divide and conquer algorithm
     *
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return min of {@code array}
     */

    public static int min(final int[] array, final int low, final int high) {
        if (array.length == 0) {
            throw new IllegalArgumentException("array must be non-empty.");
        }
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMin = min(array, low, mid); // get min in [low, mid]
        int rightMin = min(array, mid + 1, high); // get min in [mid+1, high]

        return Math.min(leftMin, rightMin);
    }

    /**
     * Get min of an array using recursion algorithm
     *
     * @param array contains elements
     * @return min value of {@code array}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure array.length > 0 before calling min().
  2. Validate low and high are within [0, array.length - 1] and low <= high.
  3. Add a wrapper that rejects empty or out-of-bounds inputs before delegating.

Example fix

// before
int result = FindMinRecursion.min(arr, 0, arr.length - 1);

// after
if (arr.length == 0) {
    throw new IllegalArgumentException("Cannot compute min of empty array");
}
if (low < 0 || high >= arr.length || low > high) {
    throw new IndexOutOfBoundsException("Invalid range [" + low + ", " + high + "]");
}
int result = FindMinRecursion.min(arr, low, high);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || array.length == 0) {
    throw new IllegalArgumentException("Array must be non-empty");
}
if (low < 0 || high >= array.length || low > high) {
    throw new IndexOutOfBoundsException("Invalid range [" + low + ", " + high + "]");
}
int result = FindMinRecursion.min(array, low, high);

Type guard

static boolean isValidRange(int[] array, int low, int high) {
    return array != null && array.length > 0
        && low >= 0 && high < array.length && low <= high;
}

Try / catch

try {
    int result = FindMinRecursion.min(array, low, high);
} catch (IllegalArgumentException e) {
    // empty array; handle gracefully
}

Prevention

When it happens

Trigger: Calling min(new int[0], 0, 0) or min(emptyArray, anyLow, anyHigh). The length check fires before any index validation, so empty-array callers always hit this message rather than an IndexOutOfBounds.

Common situations: Empty arrays from filtered or partitioned data. Recursive code paths that receive shrunk sub-arrays. Test fixtures missing initialization.

Related errors


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