From: Wolfgang Bangerth Date: Thu, 18 May 2023 04:42:58 +0000 (-0600) Subject: Properly treat exceptions in a user callback in NOX. X-Git-Tag: v9.5.0-rc1~219^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea8894f717bdea8817028c63a8d9bb90234e9edf;p=dealii.git Properly treat exceptions in a user callback in NOX. --- diff --git a/include/deal.II/trilinos/nox.templates.h b/include/deal.II/trilinos/nox.templates.h index 57d8974504..ef7b94e875 100644 --- a/include/deal.II/trilinos/nox.templates.h +++ b/include/deal.II/trilinos/nox.templates.h @@ -1054,18 +1054,23 @@ namespace TrilinosWrappers } else if (solve_with_jacobian_and_track_n_linear_iterations) { - // with tracking of linear iterations - const int n_linear_iterations = - solve_with_jacobian_and_track_n_linear_iterations(f, - x, - tolerance); - - if (n_linear_iterations == -1) - return 1; - - this->n_last_linear_iterations = n_linear_iterations; - - return 0; + // With tracking of linear iterations. The callback function we + // have here is using an integer return type, which is not + // what internal::NOXWrappers::call_and_possibly_capture_exception + // knows how to deal with. As a consequence, package the call + // and assignment of the return value into a lambda function; + // if the call to the user-defined callback fails, this will + // trigger an exception that will propagate through the lambda + // function and be treated correctly by the logic in + // internal::NOXWrappers::call_and_possibly_capture_exception. + return internal::NOXWrappers::call_and_possibly_capture_exception( + [&]() { + this->n_last_linear_iterations = + solve_with_jacobian_and_track_n_linear_iterations(f, + x, + tolerance); + }, + pending_exception); } else {