Fixes #5262 Throw exception instead of dying so that the function is testable.
Thanks for suggestion!