I have the following method defined in one of my classes:
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;
}
Later on in my program I use user input to set the values for priceSoldFor and costToCompany. Althought I set costToCompany to 0, the exception never took place. How do I fix this?