alibaba/spring-cloud-alibaba · error · ParseException

Illegal cron expression format ({})

Error message

Illegal cron expression format ({})

What it means

The generic catch-all in buildExpression: any Exception thrown during parsing that is not itself a ParseException is wrapped as ParseException with message 'Illegal cron expression format (<exception.toString()>)'. It captures unanticipated parse failures (NumberFormat, index-out-of-bounds, etc.) so callers see a uniform ParseException.

Source

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

			final TreeSet<Integer> dow = getSet(DAY_OF_WEEK);
			final TreeSet<Integer> dom = getSet(DAY_OF_MONTH);

			// Copying the logic from the UnsupportedOperationException below
			final boolean dayOfMSpec = !dom.contains(NO_SPEC);
			final boolean dayOfWSpec = !dow.contains(NO_SPEC);

			if (!dayOfMSpec || dayOfWSpec) {
				if (!dayOfWSpec || dayOfMSpec) {
					throw new ParseException(
							"Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.", 0);
				}
			}
		}
		catch (final ParseException pe) {
			throw pe;
		}
		catch (final Exception e) {
			throw new ParseException("Illegal cron expression format ("
					+ e.toString() + ")", 0);
		}
	}

	protected int storeExpressionVals(final int pos, final String s, final int type)
			throws ParseException {

		int incr = 0;
		int i = skipWhiteSpace(pos, s);
		if (i >= s.length()) {
			return i;
		}
		char c = s.charAt(i);
		if ((c >= 'A') && (c <= 'Z') && (!s.equals("L")) && (!s.equals("LW")) && (!s.matches("^L-[0-9]*[W]?"))) {
			String sub = s.substring(i, i + 3);
			int sval = -1;
			int eval = -1;
			if (type == MONTH) {

View on GitHub (pinned to 115d590110)

Solutions

  1. Inspect the wrapped exception in the message (the part in parentheses) to find the root cause (e.g. NumberFormatException: For input string "X").
  2. Correct the offending field to valid cron syntax.
  3. Run the cron through the CronExpression trial-parse validator at config load to get the exact error before deploy.

Example fix

# before (dangling dash -> NumberFormatException wrapped)
0 0 0 1- * ?

# after
0 0 0 1-31 * ?
Defensive patterns

Strategy: try-catch

Validate before calling

static String validateCron(String cron) throws ParseException {
    new com.alibaba.cloud.scheduling.schedulerx.util.CronExpression(cron);
    return cron;
}

Try / catch

try {
    new CronExpression(cron);
} catch (ParseException e) {
    // message: "Illegal cron expression format (<root cause>)"
    log.error("Cannot parse cron '{}': {}", cron, e.getMessage());
    throw new IllegalArgumentException("Invalid cron '" + cron + "'", e);
}

Prevention

When it happens

Trigger: Any malformed cron that triggers a non-ParseException during storeExpressionVals or set parsing — e.g. a non-numeric character where a digit is expected ('0 0 0 X-Y * ?'), an empty field from double spaces collapsing oddly, or an out-of-range increment value. Caught at CronExpression.java:318-320.

Common situations: Typos like '0 0 0 /5 * ?' (leading slash), '0 0 0 1- * ?' (dangling dash), or copy-paste introducing invisible characters; values outside valid ranges that surface as NumberFormatException during Integer.parseInt.

Related errors


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