I'm trying to make a custom exception, the only thing that this custom Exception has to do is to change the message of the exception but have all the attributes of the original exception.
The idea is that the message will change based on the parameter I send.
public class ServiceDireccionException : Exception
{
public ServiceDireccionException(string type) : base(GetMessage(type)) { }
public enum TypeOfService
{
Provincia,
Municipio,
Localidad
}
private static string GetMessage(string type)
{
string message = "";
switch (type)
{
case nameof(TypeOfService.Provincia):
message = ("Sucedio un error al buscar la/s" + TypeOfService.Provincia.ToString() + "/s");
break;
case nameof(TypeOfService.Municipio):
message = ("Sucedio un error al buscar lo/s" + TypeOfService.Municipio.ToString() + "/s");
break;
case nameof(TypeOfService.Localidad):
message = ("Sucedio un error al buscar la/s" + TypeOfService.Localidad.ToString() + "/es");
break;
}
return message;
}
}
When I want to use it in a try catch I can't pass the argument:
catch (ServiceDireccionException ex) //<-- It does not prompt me to pass a string.
{
throw ex;
}
catchis not the place where the exception is instantiated... and BTW you should pass the enum to the constructor instead of a string.throw-statement is the correct place where it is instantiated. So investigate on where the exception is thrown, not where you handle it.