我已经阅读了一些关于在访问器中抛出异常的优缺点的答案,但我想我会用一个例子来提出我的具体问题:
public class App {
static class Test {
private List<String> strings;
public Test() {
}
public List<String> getStrings() throws Exception {
if (this.strings == null)
throw new Exception();
return strings;
}
public void setStrings(List<String> strings) {
this.strings = strings;
}
}
public static void main(String[] args) {
Test t = new Test();
List<String> s = null;
try {
s = t.getStrings();
} catch (Exception e) {
// TODO: do something more specific
}
}
}
getStrings()
正在抛出一个尚未设置的Exception
时间。strings
这种情况是否可以通过某种方法更好地处理?