From 1e156e1e8ce7f47a06801dccda29294d050f15e1 Mon Sep 17 00:00:00 2001 From: Nikollai Date: Sun, 24 May 2026 16:32:03 +0200 Subject: [PATCH] Changed where FileNotFoundException is wrapped in an UncheckedFileNotFoundException when reading from file --- src/main/java/millions/controller/GameController.java | 3 ++- .../millions/controller/fileIO/CSV/CSVFileHandler.java | 7 ++++--- .../java/millions/controller/fileIO/StockFileReader.java | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/millions/controller/GameController.java b/src/main/java/millions/controller/GameController.java index a437790..17157dc 100644 --- a/src/main/java/millions/controller/GameController.java +++ b/src/main/java/millions/controller/GameController.java @@ -1,5 +1,6 @@ package millions.controller; +import java.io.FileNotFoundException; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.file.Path; @@ -38,7 +39,7 @@ public void startGame( player = new Player(name, startingMoney); - } catch (UncheckedFileNotFoundException e) { + } catch (FileNotFoundException e) { throw new UncheckedFileNotFoundException(e.getMessage()); } catch (InvalidFormatException e) { throw new InvalidFormatException(e.getMessage()); diff --git a/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java b/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java index 95f7b56..051e1f0 100644 --- a/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java +++ b/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java @@ -6,6 +6,7 @@ import millions.model.Stock; +import java.io.FileNotFoundException; import java.nio.file.Path; import java.util.List; @@ -28,7 +29,7 @@ public CSVFileHandler() { * @throws InvalidFormatException Throws an InvalidFormatException received from parser * @throws UncheckedFileNotFoundException Upon Receiving a FilenotFoundException */ - public List getStocksFromFile(Path filePath) { + public List getStocksFromFile(Path filePath) throws FileNotFoundException { try { StockFileReader reader = new StockFileReader(); List lines = reader.readFile(filePath); @@ -38,8 +39,8 @@ public List getStocksFromFile(Path filePath) { } catch (InvalidFormatException e) { throw new InvalidFormatException(e.getMessage()); - } catch (UncheckedFileNotFoundException e) { - throw new UncheckedFileNotFoundException(e.getMessage()); + } catch (FileNotFoundException e) { + throw new FileNotFoundException(e.getMessage()); } } } diff --git a/src/main/java/millions/controller/fileIO/StockFileReader.java b/src/main/java/millions/controller/fileIO/StockFileReader.java index f7121cf..6c8069d 100644 --- a/src/main/java/millions/controller/fileIO/StockFileReader.java +++ b/src/main/java/millions/controller/fileIO/StockFileReader.java @@ -23,7 +23,7 @@ public StockFileReader() {} * @return List of each line in the file as a string * @throws UncheckedFileNotFoundException Upon encountering a FileNotFoundException */ - public List readFile(Path path) { + public List readFile(Path path) throws FileNotFoundException { File file = new File(path.toString()); List lines = new ArrayList<>(); try (Reader reader = new FileReader(file); @@ -34,7 +34,7 @@ public List readFile(Path path) { } } catch (IOException e) { if (e instanceof FileNotFoundException) { - throw new UncheckedFileNotFoundException("Couldn't find file at specified path"); + throw new FileNotFoundException("Couldn't find file at specified path"); } else { logger.log(Level.SEVERE, "Encountered unexpected IOException: ", e.getMessage());