DEV Community


Discussion on: Exceptions are meant to be exceptional

vizanquini profile image

What do you do about input validation? I find i use exceptions the most when I have to handle input (from user or another system) and I have no idea what they can send me.
For instance, an empty string to a method where I do string manipulation. How do you let your caller know they input bad stuff? If you provide a Boolean “isSuccess()”, sure they can check for it, but you can’t be sure they will.
It’s in those cases I usually throw an e.g.: UserRegistrationException with a message like “Name not provided” to let them know

integerman profile image
Matt Eland

I think this may be a language specific scenario. For example, .NET has a ValidationException class which is used internally for server-side issues. Client-side validation hopefully does not throw an Exception, but ValidationException is often used internally for 400 series responses instead of 500 series responses.

On the .NET side, another important note is that exceptions are not just different flow, but they're expensive operations as they require the framework to grab the full call stack, so I imagine other languages have similar cautions / limitations.

In general, understand that using exceptions adds complexity to the control flow of your application, making the application quality harder to guarantee.