You can create your own Business Exception inheriting from Exception
, and make the class accept your error code. This class will be part of your domain, since it's a business exception. Nothing to do with infrastructure exceptions like DB exceptions..
public class BusinessException : Exception
{
public int ErrorCode {get; private set;}
public BusinessException(int errorCode)
{
ErrorCode = errorCode;
}
}
You can also use enums, or constants. I don't know your ErrorCode type.
In your business layer, you can throw the exception by:
throw new BusinessException(10); //If you are using int
throw new BusinessException(ErrorCodes.Invalid); //If you are using Enums
throw new BusinessException("ERROR_INVALID"); //
So after in your presentation layer you can catch this exception and process it as you need.
public void PresentationMethod()
{
try
{
_bll.BusinessMethod();
}
catch(BusinessException be)
{
var errorMessage = GetErrorMessage(be.ErrorCode);
ShowErrorUI(errorMessage);
}
}