1

I have this code that is supposed to throw a different exception with each instantiation.

Vehicle[] parkingLot = new Vehicle[5];
    try
    {
        // NEGATIVE INPUT
        parkingLot[0] = new Truck(1200.0, 2000, "Chevrolet", -8, new Person());
        // NULL MANUFACTURER
        parkingLot[1] = new Vehicle("", 8, new Person("John"));
        // NULL OWNER NAME
        parkingLot[2] = new Vehicle("Mitsubishi", 6, new Person(""));
        // TOW CAPACITY OVER 1500
        parkingLot[3] = new Truck(1700.0, 1000, "Nissan", 6, new Person("Joe"));
        // LOAD CAPACITY OVER 8600
        parkingLot[4] = new Truck(1200.0, 9000, "Nissan", 6, new Person("Frank"));
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

Since the values I'm using to create these objects are hard coded, I can't use a loop with something like

parkingLot[index]

So how ought I design my code so that when one instantiation throws an exception, attempts are still made to instantiate the rest of the objects?

Saul Femm
  • 17
  • 3
  • Honestly, the easiest way is to simply put a separate try...catch block around each one. Outside of that, you'll have to find a way to somehow parameterize your constructions so that you *can* loop over something. Why don't you check out the ["Builder Pattern"](http://en.wikipedia.org/wiki/Builder_pattern)? Then you can create an array of builders (with your hard-coded values) that will never throw exceptions, then loop over those builders to instantiate the actual objects. This seems like a great case for it anyways as your constructors are starting to take a lot of parameters. – Jason C Mar 28 '14 at 23:32
  • Wrap each assignment in an individual try/catch. It won't be pretty, but until you refactor such that constructors don't throw exceptions, your code won't get much prettier. – Asaph Mar 28 '14 at 23:32
  • (Amusingly, the Wikipedia article actually also uses vehicles as an example. Still, see the top-voted answer [here](http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern) for a great example; much better than Wikipedia IMO.) – Jason C Mar 28 '14 at 23:34

1 Answers1

1

I think that the right solution is to use seperate try...catch blocks BUT just for the challenge, this (java 8) code can do what you wanted:

Callable[] initializers = {
    // NEGATIVE INPUT
    () -> parkingLot[0] = new Truck(1200.0, 2000, "Chevrolet", -8, new Person()),
    // NULL MANUFACTURER
    () -> parkingLot[1] = new Vehicle("", 8, new Person("John")),
    // NULL OWNER NAME
    () -> parkingLot[2] = new Vehicle("Mitsubishi", 6, new Person("")),
    // TOW CAPACITY OVER 1500
    () -> parkingLot[3] = new Truck(1700.0, 1000, "Nissan", 6, new Person("Joe")),
    // LOAD CAPACITY OVER 8600
    () -> parkingLot[4] = new Truck(1200.0, 9000, "Nissan", 6, new Person("Frank"))
};

for (Callable i : initializers) {
    try {
        i.call();
    } catch (Exception ex) {
        System.out.println(e.getMessage());
    }
}
bennyl
  • 2,886
  • 2
  • 29
  • 43