Here’s the problem to solve: a method of a class that represents a gas network. This class manages objects of type Line that represent each single gas supply line.
An object of type Line is represented by the following members:
- String startCity;
- String endCity;
- int capacityUsed;
- int capacityAvail;
- int maxCapacity;
The method I’m having trouble implementing is:
boolean carry(String city1, String city2, int capacity)
Consider all the lines from city1 tocity2. For each of these lines
try using capacity with the method use() (I don’t think it’s necessary to know how
use() works ). If use() throws the exception CapacitaSuperataException search
other lines between city1 and city2, if there are no other lines use()
must return False. If a call to use() does not throw CapacitaSuperataException means that the line was assigned the capacity, and the method returns True.
I tried some solutions but I don’t know how to manage exceptions.
Thanks
Try using the try-catch inside a loop covering all suitable lines in your carry-Method: