From 423ca92d39b635f8f33599bd69578ff20d1c6391 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 2 Aug 2024 15:59:25 -0400 Subject: [PATCH] use try/catch to delete dump on dump_modify error and then rethrow exception --- src/write_dump.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/write_dump.cpp b/src/write_dump.cpp index 4bc640e084..6c932735eb 100644 --- a/src/write_dump.cpp +++ b/src/write_dump.cpp @@ -57,15 +57,18 @@ void WriteDump::command(int narg, char **arg) dumpargs[2] = arg[1]; // dump style dumpargs[3] = utils::strdup(std::to_string(dumpfreq)); // dump frequency - // delete existing dump with same ID to avoid re-use of dump-ID error - - auto *dump = output->get_dump_by_id(dump_id); - if (dump) output->delete_dump(dump_id); - for (int i = 2; i < modindex; ++i) dumpargs[i + 2] = arg[i]; - dump = output->add_dump(modindex + 2, dumpargs); - if (modindex < narg) dump->modify_params(narg - modindex - 1, &arg[modindex + 1]); + auto *dump = output->add_dump(modindex + 2, dumpargs); + + try { + if (modindex < narg) dump->modify_params(narg - modindex - 1, &arg[modindex + 1]); + } catch (LAMMPSException &e) { + // delete dump after error and then rethrow the exception to avoid re-use of dump-ID error + dump = output->get_dump_by_id(dump_id); + if (dump) output->delete_dump(dump_id); + throw e; + } // write out one frame and then delete the dump again // set multifile_override for DumpImage so that filename needs no "*"