Capturing exception messages in octave C++ API

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Capturing exception messages in octave C++ API

rupakrokade
Hello,

I am using the octave C++ API to call octave from a C++ code. I am using
"feval" to call the octave functions. Whenever something incorrect is passed
to feval, it prints a nice detailed explanation of what went wrong, on the
terminal. I want to capture this detailed message so that it can be returned
to the main function that called it, rather than just printing to the
terminal. This is because the code could be invoked by a different software
and not necessarily from the terminal. How do I do it??

Rupak



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Capturing exception messages in octave C++ API

John W. Eaton
Administrator
On 10/16/19 6:50 AM, rupakrokade wrote:
> Hello,
>
> I am using the octave C++ API to call octave from a C++ code. I am using
> "feval" to call the octave functions. Whenever something incorrect is passed
> to feval, it prints a nice detailed explanation of what went wrong, on the
> terminal. I want to capture this detailed message so that it can be returned
> to the main function that called it, rather than just printing to the
> terminal. This is because the code could be invoked by a different software
> and not necessarily from the terminal. How do I do it??

If you are using Octave development sources, error messages are now
stored in the exception object.  With earlier versions it is possible to
buffer error messages and retrieve them later, but the interface is not
very nice.

This change was made very recently.  See this thread on the maintainers
list:
https://lists.gnu.org/archive/html/octave-maintainers/2019-10/msg00009.html

jwe



Reply | Threaded
Open this post in threaded view
|

Re: Capturing exception messages in octave C++ API

rupakrokade
Thank you for your reply. I am using Octave 4.4.1 on a Debian 10 OS.  I am
not using octave development source. Currently, I get the error messages
printed on the terminal even without any try-catch statements. How do I
buffer these messages anyway? Any example code will help.

Rupak



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Capturing exception messages in octave C++ API

siko1056
On 10/17/19 4:11 PM, rupakrokade wrote:
> Thank you for your reply. I am using Octave 4.4.1 on a Debian 10 OS.  I am
> not using octave development source. Currently, I get the error messages
> printed on the terminal even without any try-catch statements. How do I
> buffer these messages anyway? Any example code will help.
>
> Rupak
>
Please find an example attached.  It is a modified version of the
embedded.cc example code in Octave which redirects std::cout and
std::cerr to an std::stringstream buffer, as described by good old
StackOverflow


https://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string

and as it was already suggested by jwe.

I tested it with Octave 4.4.1, but it should also work for Octave 5.1.0.

Anyways, the new interface of Octave dev can do this much cleaner and
easier in a one or two liner.  Thanks for this jwe!

HTH,
Kai



embedded.cc (2K) Download Attachment