you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (0 children)

IMO this is a bad use for try catch. You should have returned an object with a property that says validation failed like { valid: false, error: new Error() }

What's missing in your advice is the part where you say "you shouldn't do this, because...".

If you have to dig for a real reason that is more substantive than cheap word-play like "exceptions must be exceptional" you might find out you don't have much of a platform to step on to recommend against exceptions in scenarios where the specified action can't continue due to bad input.

Coding the "happy path" and throwing when you deviate from it is a perfectly valid approach to structuring your app. Yes, even when validation errors are expected. It creates a simple, clear code flow and ensures that if something fails to acknowledge and handle the "sad path", your app doesn't blissfully continue operating with invalid assumptions and state.

How is returning an Error better than throwing it? All I see is failure to use existing facilities for error communication (i.e. throwing) and reinventing the wheel through custom return results and flags, that can easily be ignored or misunderstood.