The context
When evaluating the following code block, the output is not inserted in the #+RESULTS
code block. This happens because of the return 1
statement.
#+begin_src cpp :results output
#include <stdexcept>
#include <iostream>
int main()
try {
throw std::runtime_error("A runtime error ocurred");
return 0;
}
catch(std::runtime_error& e) {
std::cout << "runtime_error: " << e.what() << '\n';
return 1;
}
#+end_src
#+RESULTS:
If I replace 1
with 0
, then the output is indeed shown in the #+RESULTS
code block
#+RESULTS:
#+begin_example
runtime_error: A runtime error ocurred
#+end_example
Additional context
Note that this behavior is also present in #+begin_src
whose language is sh
. Here's a minimal working example: Evaluating the following code block would result in
#+begin_src sh
f() {
return 1
}
printf "%s" "a"
f
#+end_src
#+RESULTS:
If the return value of the f
function is changed to 0
, then the output of the printf
is shown
#+begin_src sh
f() {
return 0
}
printf "%s" "a"
f
#+end_src
#+RESULTS:
#+begin_example
a
#+end_example
The question
How can I make the output of the evaluated code block be inserted in its corresponding #+RESULTS
code block regardless of the exit status of the evaluation?