Reputation: 1231
I wonder what should be returned, when some exception occurs as like in my example
public MyClass createComponent(){
String className = "foo.MyClass";
try {
final Class<? extends MyClass> clazz = Class.forName(className).asSubclass(MyClass.class);
return clazz.getDeclaredConstructor(List.class, String.class).newInstance(list, name);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
// return null; //??
}
Upvotes: 1
Views: 85
Reputation: 382132
Depending on the logic of your API and the likeness of the root exception you may
null
If the exceptions are really exceptional (they should not occur) as seems from your code, you probably should throw a runtime exception or a InternalError
stating that there's an internal error in your library. Returning null
here would do nothing good and might hide a bug.
Upvotes: 4