alibaba/spring-cloud-alibaba · error · ParseException

A numeric value between 1 and 5 must follow the '#' option

Error message

A numeric value between 1 and 5 must follow the '#' option

What it means

Thrown in checkNext when '#' in day-of-week is not followed by an integer in 1-5 (guard + catch at CronExpression.java:560-569). The value after '#' selects the 1st-5th occurrence of the weekday in the month; anything else (non-numeric, or out of 1-5) is rejected.

Source

Thrown at spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-schedulerx/src/main/java/com/alibaba/cloud/scheduling/schedulerx/util/CronExpression.java:567

			final TreeSet<Integer> set = getSet(type);
			set.add(val);
			i++;
			return i;
		}

		if (c == '#') {
			if (type != DAY_OF_WEEK) {
				throw new ParseException("'#' option is not valid here. (pos=" + i + ")", i);
			}
			i++;
			try {
				nthdayOfWeek = Integer.parseInt(s.substring(i));
				if (nthdayOfWeek < 1 || nthdayOfWeek > 5) {
					throw new Exception();
				}
			}
			catch (final Exception e) {
				throw new ParseException(
						"A numeric value between 1 and 5 must follow the '#' option",
						i);
			}

			final TreeSet<Integer> set = getSet(type);
			set.add(val);
			i++;
			return i;
		}

		if (c == '-') {
			i++;
			c = s.charAt(i);
			final int v = Integer.parseInt(String.valueOf(c));
			end = v;
			i++;
			if (i >= s.length()) {
				addToSet(val, end, 1, type);

View on GitHub (pinned to 115d590110)

Solutions

  1. Use an nth value of 1-5 after '#', e.g. '6#9' -> '6#3'.
  2. Ensure nothing trails the nth value in that field (it parses to end of token).
  3. Use 1-based counting: 1 = first occurrence, 5 = last possible.

Example fix

// before
new CronExpression("0 0 0 ? * 6#9");
// after
new CronExpression("0 0 0 ? * 6#3"); // 3rd Friday of the month
Defensive patterns

Strategy: validation

Validate before calling

// '<weekday>#<n>' requires 1 <= n <= 5.
static boolean nthOk(String dowField) {
    int h = dowField.indexOf('#');
    if (h < 0) return true;
    try {
        int n = Integer.parseInt(dowField.substring(h + 1));
        return n >= 1 && n <= 5;
    } catch (NumberFormatException e) { return false; }
}

Try / catch

try {
    CronExpression cron = new CronExpression(raw);
} catch (ParseException e) {
    throw new IllegalArgumentException("Invalid cron expression: " + raw, e);
}

Prevention

When it happens

Trigger: `new CronExpression("0 0 0 ? * 6#9")` (nth=9 > 5), or `0 0 0 ? * 6#0`, or `0 0 0 ? * FRI#x` (non-numeric). Note '#' consumes the rest of the token via s.substring(i).

Common situations: Developer uses 0 or 6+ expecting a wider range, or appends extra characters after the nth value, or a 0-based assumption ('first' = 0).

Related errors


AI-assisted analysis of alibaba/spring-cloud-alibaba@115d590110 (2026-08-14). Data as JSON: /api/errors/6f035a5591c7cdc7. Report an issue: GitHub.