Replace hard-coded asserts with exceptions.