I made 3 classes: a brand class (Marca), a comercial brand class (MarcaComercial) which is a subclass of the brand class and an exception class (ExMarcaInvalida) if one of the attributes initiated in the brand constructor is null. I want to catch this exception in my subclass constructor and declare it and use a setter method if this exception is caught. However, I can't do it because I can't initiate the values of the superclass anywhere other than the first line. Is there a way to catch the exception and do what I want? Leaving all 3 class constructors below.
public Marca(String nome, String produtor, String regiao)
throws ExMarcaInvalida {
this.nome = nome;
this.produtor = produtor;
this.regiao = regiao;
if(nome.isEmpty() || nome.isBlank()){
throw new ExMarcaInvalida("Nome invalido");
}
}
public MarcaComercial(String rotulo, String email,
String num, String nome,String produtor, String regiao)
throws ExMarcaInvalida {
try{
super(nome, produtor, regiao); //ISSUE HERE
this.rotulo = rotulo;
this.email = email;
this.num = num;
}
catch(ExMarcaInvalida e){
setRotulo("Marca Branca");
throw new ExMarcaInvalida("Marca Invalida");
}
}
public class ExMarcaInvalida extends Exception{
public ExMarcaInvalida(String msg){
super(msg);
}
}