I have a list of Method objects that I want to execute using user supplied parameters which I don't know the type or value until runtime.
I want to loop through the methods array and execute them using the invoke method until one of the methods executes successfully without an exception. Is this a good way to do it?
Method[] methods = cls.getMethods();
for (Method method : methods) {
try {
method.invoke(obj, param1, param2);
break;
}
catch(Exception e)
{
System.out.println(e);
}
}
I've been told that using try and catch for flow control is bad idea, but I'm not sure why. I'm also not sure what the alternative would be in a situation like this where I expect an exception to occur in the normal execution because the user supplies the parameters.
Any help is greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire