Space Ostrich
Space Ostrich

Reputation: 423

Continue program after Exception is caught, without using loops?

I've got a program which declares an array of Apartment objects. Each Apartments has an address, a number, a bedroom count, and a rent price. When the array is initialised, each apartment is given a string, and the Apartment class' constructor turns that string into values. The program is a test study in Exception throwing and catching using multiple classes.

Everything is currently working as it should, the Apartments are created, the string is properly converted to object parameters, and the exception is properly thrown and caught, however, currently, when the exception is caught, the program ends. I am unsure what to do about this issue. I believe that I could restructure the program so that the exception is caught inside the Apartment constructor itself, but if that doesn't work it'd be a major waste of time so I've decided to search, and then ask, on here first.

Here is the code for the "main" class in the program, with the current output below it:

public class ThrowApartmentException
{
public static void main(String[] args)
{
  // this program uses three classes, the ThrowApartmentException class is the "main" class, it's what you run to use the program. The Apartment class is used to create 
  // apartment objects, and it converts apartment Strings into values, checks those values for validity, and throws an exception if those values are wrong. This exception 
  // is an ApartmentException, which is the third class. It takes the apartment string as an argument and simply prints a message stating that the apartment failed to be 
  // instantiated.

  // this class creates an array of 6 apartment objects, with both valid and invalid values, and an appropriate message is displayed when one is instantiated successfully 
  // and one is not.

  Apartment[] apartments = new Apartment[6];
  // apartment string parameter is formatted "address, number, rooms, rent".
  try {
     apartments[0] = new Apartment("123 Fake Street, 456, 3, 1500"); // valid.
     apartments[1] = new Apartment("21 Blizzard Avenue, 333, 2, 2600"); // invalid rent.
     apartments[2] = new Apartment("6 Brr Street, 23, 1, 1000"); // invalid number.
     apartments[3] = new Apartment("25 Boat Lane, 324, 5, 1200"); // invalid rooms.
     apartments[4] = new Apartment("47 Kenneth Street, 550, 1, 1000"); // valid.
     apartments[5] = new Apartment("36 Sanders Drive, 230, 1, 1300"); // valid.
  }
  catch(ApartmentException mistake) {

  }
}
}

-----------------------------------------
Output:
Apartment 123 Fake Street, 456, 3, 1500 was successfully initialised.
Apartment 21 Blizzard Avenue, 333, 2, 2600 failed to be instantiated, one or more of the values was outside of valid range.

The current options I have open that I think may fix the problem are:

1: Place each object instantiation in it's own try/catch block.

2: Restructure the program so that the try/catch block is performed inside the Apartment constructor.

3: Be informed of some way of formatting a loop that allows for unique object instantiation like this, I could probably use an array of Strings but that seems like an incredibly awkward duct tape solution, not an actual solution.

Upvotes: 0

Views: 180

Answers (1)

Pooya
Pooya

Reputation: 6136

This is what I would do:

1- Let the Apartment Class throws the exception if an invalid input is passed to it (ApartmentException in your case)

2- Use List instead of arrays like the following:

List<Apartment> myList = new ArrayList<Apartment>();

String[] desc = new String {"123 Fake Street, 456, 3, 1500", ... }

for(int i=0; i<desc.length; i++)
{
   try
   {
      myList.add(new Apartment(desc[i]));
   }
   catch(ApartmentException mistake)
   {
      //do something
   }
}

//at this point myList contains only valid listings

Upvotes: 1

Related Questions