diff --git a/python/meep.i b/python/meep.i index a06274d79..6222c0406 100644 --- a/python/meep.i +++ b/python/meep.i @@ -1206,6 +1206,15 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c, double *total }; +%exception { + try { + $action + } catch (std::runtime_error &e) { + PyErr_SetString(PyExc_RuntimeError, e.what()); + SWIG_fail; + } +} + // Tells Python to take ownership of the h5file* this function returns so that // it gets garbage collected and the file gets closed. %newobject meep::fields::open_h5file; diff --git a/src/mympi.cpp b/src/mympi.cpp index a58c89eb3..5d5e896b0 100644 --- a/src/mympi.cpp +++ b/src/mympi.cpp @@ -121,14 +121,20 @@ double wall_time(void) { void abort(const char *fmt, ...) { va_list ap; va_start(ap, fmt); - fprintf(stderr, "meep: "); - vfprintf(stderr, fmt, ap); + char *s; + vasprintf(&s, fmt, ap); va_end(ap); - if (fmt[strlen(fmt) - 1] != '\n') fputc('\n', stderr); // force newline + // Make a std::string to support older compilers (std::runtime_error(char *) was added in C++11) + std::string error_msg(s); + free(s); + if (count_processors() == 1) { + throw runtime_error("meep: " + error_msg); + } #ifdef HAVE_MPI + fprintf(stderr, "meep: %s", error_msg.c_str()); + if (fmt[strlen(fmt) - 1] != '\n') fputc('\n', stderr); // force newline MPI_Abort(MPI_COMM_WORLD, 1); #endif - exit(1); } void send(int from, int to, double *data, int size) {