diff --git a/libsymmetrix/source/mace.cpp b/libsymmetrix/source/mace.cpp index 21c27b4..83fa382 100644 --- a/libsymmetrix/source/mace.cpp +++ b/libsymmetrix/source/mace.cpp @@ -1,5 +1,6 @@ #include //TODO #include +#include #include #include @@ -906,7 +907,16 @@ void MACE::load_from_json( const std::string filename) { std::ifstream f(filename); - nlohmann::json file = nlohmann::json::parse(f); + if (!f) { + throw std::runtime_error("Could not open model file: " + filename); + } + nlohmann::json file; + try { + file = nlohmann::json::parse(f); + } catch (const nlohmann::json::parse_error& e) { + throw std::runtime_error( + "Failed to parse model file '" + filename + "': " + e.what()); + } // Basic model information num_elements = file["num_elements"]; diff --git a/libsymmetrix/source/mace_kokkos.cpp b/libsymmetrix/source/mace_kokkos.cpp index 80ba9bd..3379c34 100644 --- a/libsymmetrix/source/mace_kokkos.cpp +++ b/libsymmetrix/source/mace_kokkos.cpp @@ -1,4 +1,5 @@ #include +#include #include // TODO: remove some of these headers? @@ -1545,7 +1546,16 @@ template void MACEKokkos::load_from_json(std::string filename) { std::ifstream f(filename); - nlohmann::json file = nlohmann::json::parse(f); + if (!f) { + throw std::runtime_error("Could not open model file: " + filename); + } + nlohmann::json file; + try { + file = nlohmann::json::parse(f); + } catch (const nlohmann::json::parse_error& e) { + throw std::runtime_error( + "Failed to parse model file '" + filename + "': " + e.what()); + } // Basic model information num_elements = file["num_elements"];