Код: Выделить всё
public class Tree {
public static Tree newInstance() throws NoWaterException, NoSoilException, NoSunshineException {
...
return new Tree();
}
}
< /code>
Когда я хочу получить экземпляр дерева, я могу сделать это: < /p>
public Tree plantTree() throws TreePlantExcetpion {
try {
...
return Tree.newInstance();
} catch (NoWaterException e) {
throw new TreePlantExcetpion("Cannot plant a tree since no water", e);
} catch (NoSoilException e) {
throw new TreePlantExcetpion("Cannot plant a tree since no soil", e);
} catch (NoSunshineException e) {
throw new TreePlantExcetpion("Cannot plant a tree since no sunshine", e);
}
}
< /code>
Но я также могу сделать это альтернативно: < /p>
public Tree plantTree() throws TreePlantExcetpion {
try {
...
return Tree.newInstance();
} catch (Exception e) {
throw new TreePlantExcetpion("Cannot plant a tree", e);
}
}
Подробнее здесь: https://stackoverflow.com/questions/493 ... subclasses