【问题标题】:Continue program after Exception is caught, without using loops?捕获异常后继续程序,不使用循环?
【发布时间】:2016-04-04 04:55:05
【问题描述】:

我有一个程序,它声明了一组 Apartment 对象。每个公寓都有地址、编号、卧室数量和租金。初始化数组时,每个公寓都被赋予一个字符串,Apartment 类的构造函数将该字符串转换为值。该程序是一个使用多个类的异常抛出和捕获的测试研究。

目前一切正常,Apartments 被创建,字符串被正确地转换为对象参数,异常被正确地抛出和捕获,然而,目前,当异常被捕获时,程序结束。我不确定如何处理这个问题。我相信我可以重组程序,以便在 Apartment 构造函数本身中捕获异常,但如果这不起作用,那将是浪费时间,所以我决定在这里搜索,然后询问首先。

这是程序中“main”类的代码,下面是当前的输出:

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.

我认为可以解决问题的当前选项是:

1:将每个对象实例化放在它自己的 try/catch 块中。

2:重构程序,以便在 Apartment 构造函数中执行 try/catch 块。

3:如果了解某种允许像这样的唯一对象实例化的循环格式化方法,我可能会使用字符串数组,但这似乎是一个非常尴尬的管道胶带解决方案,而不是实际的解决方案。

【问题讨论】:

  • 1 和 2 都应该工作。我认为 2 更好,因为它更健壮,而你 Don't Repeat Yourself。但是,这实际上并不是一个问题,所以我将投票结束(这将使您有机会将其编辑为适当的问题)

标签: java class exception


【解决方案1】:

这就是我要做的:

1- 如果将无效输入传递给Apartment Class,则让Apartment Class 引发异常(在您的情况下为ApartmentException

2- 使用 List 而不是数组,如下所示:

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

【讨论】:

  • 所以你会有一组公寓信息字符串?我想如果这将是一个实际程序而不仅仅是一个练习,用户输入,如果它是一种“脆弱”的格式,将被存储为一个数组。我会试一试。我仍然不太确定自定义异常的最佳实践是什么。编辑:好吧,数组或列表。
  • @SpaceOstrich 因为您的 Apartment 构造函数将 String 作为输入,您最终必须将用户输入转换为 String
  • 有趣的是,这个程序没有用户输入。一个假设的实际示例可能是一些自动公寓登记的一部分,检查它们的有效性或价格/卧室范围。所有输入都是在运行前通过实例化的对象字符串进行的。我可能会重新格式化这个问题,使其更具体。
  • @SpaceOstrich 你使用什么机制,(从用户读取数据,爬取其他站点,......)在某些时候你需要转换你的数据以适应你的公寓类的构造函数(字符串,或其他一些数据类型)
  • 将此标记为已回答,因为我确实解决了问题,并且确实从问题中获得了一些有用的信息。但是,除了我以外的任何人都看不到它,所以我赞成将其关闭。
猜你喜欢
  • 2020-07-01
  • 2011-02-07
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多