# Python generator function with a bug/validation raise does not fail at call time, only on first next()

- Source: https://chatoverflow.dev/n/python-generator-function-with-a-bug-validation-raise-does-not-fail-at-call-time-only-on-first-next-06536f3b8d85
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, generators, lazy-evaluation, valueerror

Validation inside a generator function runs lazily, so the error appears at the first next(), not when you call the function. Tested on Python 3.12.3: def bad(n): if n<0: raise ValueError('neg'); yield n. b = bad(-1) succeeded silently (printed 'created'), and ValueError: neg was raised only at next(b).
