diff --git a/libinterp/parse-tree/oct-parse.in.yy b/libinterp/parse-tree/oct-parse.in.yy --- a/libinterp/parse-tree/oct-parse.in.yy +++ b/libinterp/parse-tree/oct-parse.in.yy @@ -5370,35 +5370,38 @@ s = evalc ("t = 42"), t octave_value_list retval; int eval_nargout = std::max (0, nargout - 1); - const octave::execution_exception* eval_exception = 0; try { retval = Feval (args, eval_nargout); } - catch (const octave::execution_exception& e) + catch (const octave::execution_exception&) { - // hold back exception from eval until we have restored streams - eval_exception = &e; + // Stop capturing buffer and restore stdout/stderr. + out_stream.flush (); + err_stream.flush (); + + out_stream.rdbuf (old_out_buf); + err_stream.rdbuf (old_err_buf); + + // Print error message again, which was lost because of the + // stderr buffer Note: this keeps error_state and + // last_error_stack intact + + message_with_id ("error", last_error_id ().c_str (), + last_error_message ().c_str ()); + + throw; } - // stop capturing buffer and restore stdout/stderr + // Stop capturing buffer and restore stdout/stderr. out_stream.flush (); err_stream.flush (); out_stream.rdbuf (old_out_buf); err_stream.rdbuf (old_err_buf); - if (eval_exception) - { - // Print error message again, which was lost because of the stderr buffer - // Note: this keeps error_state and last_error_stack intact - message_with_id ("error", last_error_id ().c_str (), - last_error_message ().c_str ()); - // rethrow original exception from above - throw *eval_exception; - } - retval.prepend (buffer.str ()); + return retval; }