【发布时间】:2014-02-08 15:25:47
【问题描述】:
我在 C++ 中为 Octave 编写了一个动态链接函数,名为“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;
}
}
我使用“throw”抛出异常来模拟“hello”中可能发生的任何错误。我在 Octave 终端中使用“system”“mkoctfile”编译“hello”。
octave:2> system('mkoctfile /home/b/Desktop/hello.cc')
ans = 0
“system”返回“0”表示没有发现错误。我在 Octave 终端中将“hello”作为动态链接函数运行,这是我收到的终端消息:
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
每次我执行“hello”的测试运行时,是否可以捕获错误并防止 Octave 终止?我将非常感谢一个小的示例代码,它抛出一个豁免并允许 Octave 继续运行而不会终止,如果我能显示一条关于哪里或什么导致错误的消息,那就更好了。谢谢。
==== 2014 年 2 月 9 日添加 ==================================== ========================
感谢 Carandraug 向我解释终止异常和非终止异常之间的区别。下面的代码是我遇到的非故意终止异常的示例。使用 Carandraug 提供的代码,我添加了第 8 行和第 9 行,它们模拟了意外终止异常,以模拟我编写的代码出现致命错误。
#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;
}
使用 Carandraug 关于为 Octave 编译动态链接函数的建议,我遇到了以下 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)
我根据 Carandraug 提供的信息编辑了带有“终止异常”一词的问题标题。如果我必须将修改后的问题移到自己的帖子中,请告诉我。
当我遇到这个错误时,我做了一个粗略的假设,即我会无意中在代码中创建其他致命错误,从而导致 Octave 终止。我预见这可能是一个令人讨厌的调试过程,尤其是如果终止后的 Octave 不会提供有关导致错误的原因的更多信息。但是,我相信它们可能只是一小部分可以编写的错误,这些错误太明显而无法导致终止异常,或者这只是编写不具有“安全性”的 c++ 代码的本质。网络框架”我已经习惯了。
【问题讨论】: