I'm developing an RPC Web Service that offers methods to clients and I want to use Exceptions to handle errors within the server application, as to automatically generate a response with an error header and, more important, a custom error code (integer). I have created a base CustomException class which holds an integer and the message.
Should I create a new class for every exception on every method, with its custom error code hard-coded on the class? Or is there another approach to this problem?
e.g. let's suppose I have a Login method which can have two exceptions, "UnknownUsername" and "InvalidPassword" (just as an example; as Tieston T. commented, it's not a good practice to return information on authentication). The approaches I can think of so far are:
- Using a
LoginException
class and setting the error code on the internal method - Creating a
UnknownUsernameException
and anInvalidPasswordException
class, inheritingLoginException
, and hard-code the error code on each class