0

我在我的一个类中定义了以下方法:

public String calculateProfitMargin() {
      String returnMe = "";
      double profitMargin = 0;
      try {
         profitMargin = (orderItems[0].getItem().getPriceSoldFor() - orderItems[0].getItem().getCostToCompany()) / ( (int) orderItems[0].getItem().getCostToCompany() );
      }
      catch (ArithmeticException e) {
         System.out.println("\nEXCEPTION CAUGHT\n________________");
         System.out.println("Details: / by Zero");
         System.out.println("To prevent this problem in the future DO NOT enter 0 as the cost to the company for any product in the system");
         System.exit(0);
      }
      returnMe = "The profit margin for Order " + orderNumber + " is: " + (profitMargin * 100) + "%";
      return returnMe;
   }

稍后在我的程序中,我使用用户输入来设置 priceSoldFor 和 costToCompany 的值。尽管我将 costToCompany 设置为 0,但从未发生异常。我该如何解决?

4

0 回答 0