If there is an exception, you should propagate it and let another method deal with the problem, this is not the right way to deal with it. You’ll have to deal with the problem, do it right or wrong.
If you want to insist, I believe that the best way is to create a class just for this (since Java does not give many facilities) whose members will be this list of products and the flag, then in return you break up the class, ie, take each of the members and use as you like (play in variable, use in if
, etc..).
Note that I disagree with each line of this solution but it would be something more or less like this (the codes are not ready for use, it’s just a general idea):
public class Resultado {
public boolean flag; //não faça isto, fiz só para simplificar
public listProdutos[] produtos; //prefira usar métodos de acesso get e set
public Resultado(listProdutos[] produtos, boolean flag) {
this.produtos = produtos;
this.flag = flag;
}
}
public Resultado getProdutosWS() {
try{
listProdutos[] produtos = ws.getProdutos(prod)
return new Resultado(produtos, true);
}catch(RemoteException ex){
return new Resultado(null, false);
}
}
public static main() {
Resultado lista = getProdutosWS();
if (lista.flag) {
System.out.println(lista.produtos[0]); //isto é só um exemplo de uso, nada que seja útil
}
}
The more I look at this the more I find it strange. I think you should treat the exception, for this example, at main
, something like:
public AlgumMetodoConsumidor(TipoDoWS ws, TipoDoProd prod) {
try {
listProdutos[] lista = ws.getProdutos(prod)();
System.out.println(lista); //isto é só um exemplo de uso, nada que seja útil
} catch (RemoteException ex) {
//regra básica das exceções: você lida aqui com o problema porque é
//aqui que você consegue lidar com ele.
System.out.println("deu erro");
}
}
Alternative to not having to create a specific class for this. The advantage would be that the class Resultado
would be used for any site that needs to return a flag in addition to another return:
public class Resultado {
public boolean flag; //não faça isto, fiz só para simplificar
public Resultado(boolean flag) {
this.flag = flag;
}
}
public Resultado getProdutosWS(Resultado resultado) {
try{
listProdutos[] produtos = ws.getProdutos(prod)
resultado.flag = true;
return produtos;
}catch(RemoteException ex){
resultado.flag = false;
return null;
}
}
public static main() {
Resultado resultado;
listProdutos[] lista = getProdutosWS(resultado);
if (resultado) {
System.out.println(lista[0]); //isto é só um exemplo de uso, nada que seja útil
}
}
I put in the Github for future reference.
On the other hand I do not know if it would not be enough to check whether the list is null
. Of course, it depends on the situation.
Reserve the return position 0 for the flag.
– Guill
What is the class structure
listProdutos
?– Guill
consists of 3 strings || constructors || get’s and set’s (you can tell!?)
– jsantos1991
If there is an exception, you should propagate it, this is not the right way to deal with it. If you want to insist I believe that the best way is to create a class just for this (since Java does not give many facilities) whose members will be this list of products and the flag, then in return you break up the class, ie, take each of the members and use as you like (play in variable, use in
if
, etc..).– Maniero
@bigown I’ve been researching this and I’ve seen this option, but it seemed a little strange(create a class just for that!?) could you explain better that of propagating the exception? I tried to catch where I wanted but could not
– jsantos1991
@jsantos1991 I agree with you that it is strange but it is the solution if you want to insist on the error. At least in Java I see no other way to solve this. I insist that you must resolve this by letting the exception overflow into the method that knows how to treat the problem properly. What you’re trying to do is even stranger. See that answer that I gave and goes on all the links of her and the others you follow to learn more about exception (worth even what is not about Java).
– Maniero
@jsantos1991 You are not required to propagate communication exception. Carefully note this recommendation that you "should" propagate it. The way you are trying to do it is not necessarily a mistake.
– Caffé
@jsantos1991 read everything I’ve given you and then you tell me if you gain anything dealing with the exception before the moment you can do something with it and turn it into flag. If you win something real, if you can show that you have a plausible justification, go ahead, otherwise you’re doing over Engineering. Never treat an exception when you can’t do anything useful with it. This is a basic rule of exception.
– Maniero