I'm fighting with this java code about exceptions.
What I want to do is create a method that validates if the arguments that happen are correct, but make the exception. the problem is that this leads me to add an exception in the setter, and if I add it to this, it throws me that the constructor needs exception.
public class Contribuyente
{
private String sexo;
public Contribuyente(String sexo){
this.setSexo(sexo);
}
public void setSexo(String sexo)
{
validarSexo(sexo);
this.sexo=sexo;
}
public static boolean validarSexo(String sexo)throws Exception
{
boolean valido=true;
if((!"F".equals(sexo))||(!"M".equals(sexo)))
{
valido=false;
}
if (valido==false)throw new Exception("Sexo invalido");
return true;
}
}
the code is longer, I only leave the part that I have problem