diff --git a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp index 70e43e72dd..21a9b58af7 100644 --- a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp @@ -11,20 +11,37 @@ #include #include +#include "frc/Errors.h" + using namespace frc; using namespace frc::sim; SimDeviceSim::SimDeviceSim(const char* name) - : m_handle{HALSIM_GetSimDeviceHandle(name)} {} + : m_handle{HALSIM_GetSimDeviceHandle(name)} { + if (m_handle == 0) { + throw FRC_MakeError(err::InvalidParameter, + "No sim device exists with name '{}'.", name); + } +} SimDeviceSim::SimDeviceSim(const char* name, int index) { m_handle = HALSIM_GetSimDeviceHandle(fmt::format("{}[{}]", name, index).c_str()); + if (m_handle == 0) { + throw FRC_MakeError(err::InvalidParameter, + "No sim device exists with name '{}[{}]'.", name, + index); + } } SimDeviceSim::SimDeviceSim(const char* name, int index, int channel) { m_handle = HALSIM_GetSimDeviceHandle( fmt::format("{}[{},{}]", name, index, channel).c_str()); + if (m_handle == 0) { + throw FRC_MakeError(err::InvalidParameter, + "No sim device exists with name '{}[{},{}]'.", name, + index, channel); + } } SimDeviceSim::SimDeviceSim(HAL_SimDeviceHandle handle) : m_handle(handle) {} diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java index a64e2f33af..bd86f16fee 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java @@ -25,6 +25,9 @@ public class SimDeviceSim { */ public SimDeviceSim(String name) { this(SimDeviceDataJNI.getSimDeviceHandle(name)); + if (m_handle == 0) { + throw new IllegalArgumentException("No sim device exists with name '" + name + "'."); + } } /**