Centralize the logic for throwing exceptions. Fixes #5913.