I wrote a dynamically linked function for Octave in c++ called "hello".
#include <octave/oct.h>
DEFUN_DLD (hello, args, ,"Print 42") {
try{
octave_value retval ;
throw ;
return retval = 42.0 ;
}
catch(...){
std::cerr << "An error has occurred!" << std::endl;
}
}
I throw an exception using "throw" to simulate any possible error that can occur in "hello". I compile "hello" using "system" "mkoctfile" in the Octave terminal.
octave:2> system('mkoctfile /home/b/Desktop/hello.cc')
ans = 0
"system" returns "0" to indicate that no errors were found. I run "hello" as a dynamically linked function in the Octave terminal and this is the terminal message I receive:
octave:3> hello
terminate called without an active exception
panic: Aborted -- stopping myself...
attempting to save variables to 'octave-workspace'...
save to 'octave-workspace' complete
Aborted
Is it possible to catch the error and prevent Octave from terminating every time I perform a test run of "hello"? I would be most grateful for a small sample code that throws an exemption and permits Octave to continue to run without terminating and even better if I can display a message of where or what caused the error. Thanks.
==== Added on Feb. 9, 2014 ===========================================================
Thanks Carandraug for explaining to me the difference between a terminating exception and a non-terminating exception. The code below is an example of a terminating exception I encountered that was not intentional. Using the code provide by Carandraug I added lines 8 and 9 which simulate an unintentional terminating exception in order to simulate me writing code with a fatal error.
#include <octave/oct.h>
DEFUN_DLD (hello, args, ,"Print 42") {
octave_value retval;
try {
Matrix matrix_temp ; // line 8
matrix_temp(-1,-1) = 1; // line 9
}
catch(...) {
error ("An error has occurred!");
return retval;
}
return retval = 42;
}
Using Carandraug's suggestions on compiling Dynamically Linked Functions for Octave I encountered the following Octave Termination:
octave:3> mkoctfile /home/gin/Desktop/hello.cc
octave:4> hello
*** glibc detected *** octave: double free or corruption (out): 0x0000000002225740 ***
======= Backtrace: =========
...
octave[0x400561]
======= Memory map: ========
...
pack.so.3gf.0panic: Aborted -- stopping myself...
attempting to save variables to `octave-core'...
save to `octave-core' complete
Aborted (core dumped)
I edited the question header with the word "terminating exception" based on the information provided by Carandraug. Let me know if I have to move the revised question into its own post.
When I encountered this error I made the gross assumption that I would unintentionally create other fatal errors in the code that would cause Octave to terminate. I foresaw this as a possible annoying debugging process, especially if Octave after termination would not provide more information on what caused the error. However, I believe it is possible that either their are only a small handful of possible errors that can be written which are too obvious to make that cause terminating exceptions or this is simply the nature of writing c++ code which does not have the "safety .net frame work" I am used to.