alibaba/spring-cloud-alibaba · error · ParseException

'#' option is not valid here. (pos={i})

Error message

'#' option is not valid here. (pos={i})

What it means

Thrown in checkNext when '#' (nth weekday) follows a value but the field is not DAY_OF_WEEK (guard at CronExpression.java:556-557). '#N' means 'the Nth <weekday> of the month' and is only valid in day-of-week (e.g. '6#3' = 3rd Friday).

Source

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

		if (c == 'W') {
			if (type == DAY_OF_MONTH) {
				nearestWeekday = true;
			}
			else {
				throw new ParseException("'W' option is not valid here. (pos=" + i + ")", i);
			}
			if (val > 31) {
				throw new ParseException("The 'W' option does not make sense with values larger than 31 (max number of days in a month)", i);
			}
			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;

View on GitHub (pinned to 115d590110)

Solutions

  1. Use '<weekday>#<n>' only in day-of-week, e.g. '0 0 0 ? * 6#3'.
  2. For day-of-month use plain numbers or ranges, not '#'.
  3. Recheck the six-field ordering.

Example fix

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

Strategy: validation

Validate before calling

// '#' is valid only in the day-of-week field.
static boolean hashInDowOnly(String[] fields) {
    if (fields.length < 6) return false;
    return !fields[0].contains("#") && !fields[1].contains("#")
        && !fields[2].contains("#") && !fields[3].contains("#")
        && !fields[4].contains("#");
}

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 5#1 * ?")` ('#' in day-of-month), or `0 0 5#1 * ? *` ('#' in hours). Any '<n>#<m>' outside day-of-week.

Common situations: Developer confuses '#' with a list separator or places the nth-weekday expression in the day-of-month field. Field-count mistakes also relocate it.

Related errors


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