I was just wondering if it’s a best practice to throw errors on validation and logic like the one below. I am catching these in my OnException method in my controller and sending it back to the client via Ajax as JSON. Is throwing exceptions like this ok?
public void Update(EditTeacherModel model)
{
var entity = _teachersRepository.FindBy(model.Id);
if(entity == null)
throw new NatGeoNotFoundException("Teacher");
}
As long as you have a special, clean exception type for this, I don’t see a reason why this would be a problem. Exceptions are a convenient way to step out of multiple nested call stack frames.
Be aware, though, that exception are very slow on the CLR.