I have a Membership exception which looks like this:
public enum MembershipError
{
EmailNotFound,
EmailNotConfirmed,
IncorrectPassword,
EmailExists
}
public class MembershipException : ApplicationException
{
public MembershipError MembershipError { get; set; }
public MembershipException(MembershipError membershipError)
: base(Enum.GetName(typeof (MembershipError), membershipError))
{
MembershipError = membershipError;
}
}
Should I use an enum in my exception or make an exception for each enum? Because then I would be putting logic when catching the exception like this:
try
{
}
catch (MembershipException exception)
{
switch (exception.MembershipError)
{
case MembershipError.EmailExists:
break;
//etc.
}
}
My service layer throws these exceptions, the web layer/in the action catches these, generate the proper json and return it to the view.
Suggest an alternative please?
Exceptions should only be used for exceptional situations. The errors listed in your enumeration appear to be fairly standard and I would choose not to express them through an exception. Instead I would prefer
TryXXXstyle API over exceptions.For example