Remove useless code and throw the right exception.