I understand that “Exceptions for exceptional cases” [a], but besides the fact that they are repeated over and over , I never found a real reason for this fact.
Since they stop execution, it makes sense that you do not want them for simple conditional logic, but why not introduce a check?
Say that you had to go through a group of inputs and catch each exception in order to group them together to notify the user ... I constantly see that this is somehow "wrong" because users enter the wrong input all the time, but this point seems based on semantics .
Entrance is not expected and therefore is exceptional. Throwing an exception allows me to pinpoint what was wrong, like StringValueTooLong or IntegerValueTooLow or InvalidDateValue or something else. Why is this considered wrong?
An alternative to throwing an exception is either returning (and, ultimately, collecting) the error code, or even worse is the error string. Then I would either show these error lines directly, or analyze the error codes, and then display the corresponding error messages to the user. Wouldn't an exception be considered a malleable error code? Why create a separate table of error codes and messages when they can be generalized using the exception function already built into my language?
Also, I found this Martin Fowler article on how to handle such things - a notification template. I am not sure how I see this as anything other than Exceptions, which do not stop execution.
a: Everywhere I read something about Exceptions.
--- Edit ---
Many wonderful comments were made. I have commented on most and good reviews, but I am not yet fully convinced.
I don’t want to defend Exceptions as the proper means to resolve Input Validation, but I would like to find good reasons why the practice is considered so evil when it seems that most of the alternative solutions are just masked Exceptions.
validation exception error-handling
enobrev Jan 04 '09 at 6:25 2009-01-04 06:25
source share