Я не уверен, что лучший способ бросить несколько исключений в основном методе. Это подход, который я взял, мне было интересно, если этот путь является правильнымJAVA: Try catch Исключения
public static void main(String[] args) {
File nFile = new File("ProductData.txt");
File file = new File("CustomerData.txt");
File pFile = new File("PurchaseOrderDataFile.txt");
try {
Scanner pScan = new Scanner(pFile);
Scanner scan = new Scanner(file);
//Makes ElectronicsEquipmentSupplier object with the month and year product array
ElectronicsEquipmentSupplier ees = new ElectronicsEquipmentSupplier
(1, 12, InputFileData.readProductDataFile(nFile));
//Adds successive customer records to suppliers customer list
for (int i = 0; i < 28; i++) {
ees.addNewCustomer(InputFileData.readCustomerData(scan));
}
for (int i = 0; i <= 24; i++) {
String poByMonth = InputFileData.readPurchaseOrderDataFile(pScan); //Brings list in by months
String[] purchaseOrder = poByMonth.split("\\s+");
ees.startNewMonth(); //When the months are split by the @ it adds a new month
for (int j = 0; j <= purchaseOrder.length - 1; j++) {
String[] result = purchaseOrder[j].split("#");
int qty = Integer.parseInt(result[3]);
ees.addNewPurchaseOrder(result[0], result[1], result[2], qty);
double orderTotal = 0;
for (Product p : ees.getRangeOfProducts()) {
if (p.getProductCode().equals(result[2])) {
orderTotal = p.getPricePerUnit() * qty;
}
}
CustomerDetails customer = ees.getDetails().findCustomer(result[1]);
customer.setTotalPrice(orderTotal + customer.getTotalPrice());
if (result[1].substring(0, 1).equals("P")) {
System.out.println("Customer ID: " + (result[1]));
System.out.println("Discount: " + customer.getDiscountRate());
}
}
}
} //Catches exceptions
catch(IllegalCustomerIDException| IllegalProductCodeException |
IncorrectPurchaseOrderException | CustomerNotFoundException | IOException ex){
//Outputs exceptions if they are caught
System.out.println(ex);
}
}
Как вы можете видеть, что я положил все это в один большой попытке поймать и бросить все исключения сразу. Мне показалось, что это отличный способ сделать это, но я не уверен, действительно ли это хорошая практика.
Почему бы вам просто не объявить, что основной метод выбрасывает все эти типы исключений? –
Как это сделать? если вы имеете в виду положить все их в заголовок? Мне сказали, что это была плохая практика. –
'public static void main (String [] args) throws IllegalCustomerIDException, IllegalProductCodeException' и т. Д. –