【问题标题】:How to write a statement to check for my user specified exception CustomerNotEnrolledException如何编写语句来检查我的用户指定的异常 CustomerNotEnrolledException
【发布时间】:2015-03-01 18:06:40
【问题描述】:

我之前写了一个完整的成功程序,由7个类(DateAddressTimeCustomerCourseInClassCourseOnLineCourse),一个接口(@98​​7654328@)组成) 当然还有测试类 (CustomerTest)。接口Invoice 有一个方法createInvoice,它在Customer 类中实现。在测试用例中,我创建了 3 个新客户,添加了他们各自的课程,然后根据他们注册的课程数量、他们注册的课程类型以及课程是否为 InClassCourseOnLineCourse,最后打印出信息对话框。客户和课程列表保存在两个单独的数组列表中:

ArrayList<Customer> customerList = new ArrayList<Customer>();
ArrayList<Course> courseList = new ArrayList<Course>();

使用增强的 for 循环,我以多态方式遍历 customerList 并为每个客户创建发票。 我现在编写了一个附加类CreateFiles,其中包含一个新的客户和课程列表,它将客户数据写入文件 customers.txt 并将课程数据写入文件 courses.txt强>。 CreateFiles 有一个方法 writeCustomerswriteCourses

我不熟悉异常并且仍在学习。要修改我的程序,我想添加一个名为 CustomerNotEnrolledException 的用户指定异常。 Customer 类中的createInvoice 方法如果客户在他的列表中没有任何课程(我的几个客户没有注册任何课程)将抛出CustomerNotEnrolledException,然后在我的测试类中处理这个异常。

我的问题是如何在 try 块中编写语句来检查客户是否注册了任何课程,如果没有取消它们。我需要这样做,因为在我已经淘汰了未注册的客户,我将在Test case中添加方法readCustomersreadCoursesgenerateInvoice,并使用它们来读取customers.txt文件和courses .txt 文件来创建客户并将他们添加到customerList,以及创建课程,这些课程将添加到他们各自的客户中。

我已经创建了一个名为 CustomerNotEnrolledException 的异常类,它扩展了异常:

  public class CustomerNotEnrolledException extends Exception
  {
      public CustomerNotEnrolledException()
      {
         super("Customer not enrolled");
      }

我原来的 createInvoice 方法是这样的:

    public String createInvoice() 
    {       
            double total;

        if (cType==CustomerType.STUDENT)
            total = 25;
               else if (cType==CustomerType.FACULTY)
            total = 50;
               else if (cType==CustomerType.GOVERNMENT)
            total = 35;
        else
            total = 0;

       for(Course course:this.courseList)
       {
           total += course.calculateCharge(this.cType); 
       }  

           return (String.format("%s\t\t%d\t\t$%.2f", this.name,      
               this.accountNumber,total));    
    }

【问题讨论】:

  • 不要描述我们的代码,而是发布它的相关部分,并发布您想要编写的方法及其 javadoc 描述它应该做什么。

标签: java exception arraylist exception-handling throws


【解决方案1】:

首先,修改你的方法createInvoice()如下:

public String createInvoice() throws CustomerNotEnrolledException {

    if ((this.courseList == null) || (this.courseList.isEmpty())) {
        throw new CustomerNotEnrolledException("Customer does not have any course");
    }

    // rest of your method goes here
}

然后,在每个调用createInvoice() 方法的类中,您必须在调用周围加上一个捕获CustomerNotEnrolledException 的try 块:

Customer customer = ...; // get the customer from some place
try {

    // some code here

    customer.createInvoice();

    // more code here

} catch (CustomerNotEnrolledException e) {
    // handle CustomerNotEnrolledException here, maybe show error message?
    System.out.println("Exception creating invoice for customer " + customer.getName() + ": " + e.getMessage());
}

这个想法是createInvoice() 抛出一个CustomerNotEnrolledException 如果他的课程列表中没有任何课程。这只是对应该发生什么的猜测,因为我事先并不知道问题所在。此外,我认为正确实现逻辑是您的工作。

由于createInvoice()方法抛出一个checked异常,在这种情况下CustomerNotEnrolledException,调用createInvoice()的方法必须处理CustomerNotEnrolledException异常,可以使用我在示例中显示的 try/catch 块,或者通过声明该方法在其签名中抛出 CustomerNotEnrolledException(就像 createInvoice() 方法一样)。

【讨论】:

  • 尝试块会是尝试进入客户的位置吗?
  • @AnastaciaKong 不一定,try 块必须围绕对createInvoice() 的调用。您可以在调用createInvoice() 的相同方法中进行环绕,或者在调用调用它的方法的方法中进行,或者在调用调用它的方法的方法中调用,等等。在极端情况下在这种情况下,您可以在 public static void main() 方法中使用 try 块,在最极端的情况下,您可以声明您的 public static void main() 方法抛出 CustomerNorEnrolledException
  • 我在这里理解的问题是,当您说 try 块必须围绕对 createInvoice 的调用时。我在原始测试用例中调用了一次 createInvoice,其目的是检索每个客户计算的发票。代码行如下所示: for(Customer c:customerList) { output += c.createInvoice() + "\n"; System.out.println(c.toString()); System.out.println(c.getCourseList() + "\n"); } JOptionPane.showMessageDialog(null, output);
  • 好的,如果这是你应该做的,用一个 try 块包围你所有的测试用例的代码并捕获 CustomerNotEnrolledException 以正确处理它(取决于你在这个时候必须做什么出现异常情况)。不要忘记让你的方法声明它们抛出这个异常,以便异常可以到达你的 main 方法。
  • 好吧,我忘了告诉你一件事。对不起,我正在部分修改这个程序(#1、#2 和#3)。 #2 是添加异常并将其扔到要处理的主要部分的地方,但我只看了 #3 的最后一部分并错过了一个基本部分。我应该在我所做的测试用例中创建一个名为 generateInvoice 的方法。此方法多态地创建要在我的对话框中显示的发票。 generateInvoice 方法处理 CustomerNotEnrolled 异常。当引发此异常时,它会向系统提示符打印一条消息。
【解决方案2】:

我相信你可以将 'Exception' 扩展成一个看起来有点像这样的新创建的类。

public class CustomerNotEnrolledException extends Exception {

      public CustomerNotEnrolledException(String message) {
           super(message);
      }

      public CustomerNotEnrolledException(String message, Throwable throwable) {
           super(message, throwable);
      }

}

然后将此异常导入您打算在其中使用它的类。

【讨论】:

  • 谢谢,我已经创建了这个类。我只需要知道如何修改我的方法 createInvoice 或者是否需要在 main 中创建一个新方法来检查客户是否注册了任何课程。
  • 您能否提供您的 createInvoice 方法和 main 以便我明白您的意思?您只想像使用其他任何异常一样使用此异常(尝试 - 捕获)。
  • 请编辑您的初始帖子并将此代码添加到代码块中以便于阅读。
  • 好的,我编辑了它,并将我想要解决的问题以粗体显示。如果你理解得更好,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2018-01-24
  • 2011-03-01
  • 2011-11-16
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多