Start Validating articles

Validating articles

-- Dave Thomas and Andy Hunt An important consequence of this is that whether to use exceptions for a particular task is dependent on the context.

If you are trying to read a well known file location, such as on a unix system, then it's likely you can assume the file should be there, so throwing an exception is reasonable.

On the other hand if you are trying to read a file from a path that the user has typed in on the command-line, then you should expect that it's likely the file isn't there, and should use another mechanism - one that communicates the unexceptional nature of the error.

When we're doing a refactoring, the rule is to avoid changes in observable behavior.

In situations like this, such a rule leads immediately to the question of what behavior is observable.

I've not mentioned the example domain so far, since I was just interested in the broad shape of the code.

But as we explore the example further, I'll need to engage with the domain.

Assume that an uncaught exception will terminate your program and ask yourself, "Will this code still run if I remove all the exception handlers?

" If the answer is "no", then maybe exceptions are being used in nonexceptional circumstances.

There is a case when it may be sensible to use exceptions for validation failures.

This would be situations where you have data that you expect to have already been validated earlier in processing, but you want to run the validation checks again to guard against a programming error letting some invalid data slip through.

Gson takes a class, looks for any fields that match a key in the JSON document, and then populates the matching fields.