From f2b0a627b72ede8b3e9b6c4ee14527d16b371909 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Wed, 13 Sep 2023 21:45:37 -0400 Subject: [PATCH] Make loadAprilTagFieldLayout throw an unchecked exception instead --- .../edu/wpi/first/apriltag/AprilTagFieldLayout.java | 3 +++ .../java/edu/wpi/first/apriltag/AprilTagFields.java | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java index 2c9bc4769c5..b4317e4d322 100644 --- a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java +++ b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java @@ -202,6 +202,9 @@ public static AprilTagFieldLayout loadFromResource(String resourcePath) throws I try (InputStream stream = AprilTagFieldLayout.class.getResourceAsStream(resourcePath); InputStreamReader reader = new InputStreamReader(stream)) { return new ObjectMapper().readerFor(AprilTagFieldLayout.class).readValue(reader); + } catch (NullPointerException e) { + // Class.getResourceAsStream() returns null if the resource does not exist. + throw new IOException("Could not locate resource: " + resourcePath + "\n"); } } diff --git a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java index be9caecb9ee..89d256e1a4a 100644 --- a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java +++ b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java @@ -5,6 +5,7 @@ package edu.wpi.first.apriltag; import java.io.IOException; +import java.io.UncheckedIOException; public enum AprilTagFields { k2022RapidReact("2022-rapidreact.json"), @@ -25,9 +26,13 @@ public enum AprilTagFields { * Get a {@link AprilTagFieldLayout} from the resource JSON. * * @return AprilTagFieldLayout of the field - * @throws IOException If the layout does not exist + * @throws UncheckedIOException If the layout does not exist */ - public AprilTagFieldLayout loadAprilTagLayoutField() throws IOException { - return AprilTagFieldLayout.loadFromResource(m_resourceFile); + public AprilTagFieldLayout loadAprilTagLayoutField() { + try { + return AprilTagFieldLayout.loadFromResource(m_resourceFile); + } catch (IOException e) { + throw new UncheckedIOException("Could not load AprilTagFieldLayout from " + m_resourceFile, e); + } } }