Three Reasons Not to use Exceptions for Model Validation

Uno. The semantic problem

Exceptions "mean" that something out of the ordinary has happened. A validation error is part of the normal program flow, it is not "exceptional". New developers joining an existing team will likely be confused when they see exceptions used inappropriately.

Duo. The performance problem

Throwing exceptions is slow. Validation errors will occur all the time so performance is a reasonable consideration.

Tre. The Try... Catch problem

If you use exceptions to return model validation errors then somewhere you are going to need a Try... Catch... construct, which makes your code ugly.

... Many code bases are completely dominated by error handling ... it is nearly impossible to see what the code does because of all of the scattered error handling. - Michael Feathers, Clean Code


Comments are closed