- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在我的应用程序中使用 Qt 脚本引擎作为用户访问其功能的替代方式。因此,我将一些 C++ 类导出到 Qt ScriptEngine,它将作为应用程序的接口(interface)。问题是,这些 C++ 类会抛出异常。
我有一个“ScriptInterface”类在它自己的线程上运行,监听处理脚本的请求。因此,当我评估用户的脚本时,我在它周围有一个 try/catch block 来处理异常,并将错误打印到应用程序的控制台。
...
try {
m_engine->evaluate(script, name);
}
catch (Exception const& e) {
// deal with it
}
catch (...) {
// scary message
}
这在 Windows 中完美运行...但在 Linux 中不起作用 - 程序终止并显示以下消息:
terminate called after throwing an instance of 'Basilisk::InvalidArgumentException'
what(): N8Basilisk24InvalidArgumentExceptionE
Aborted
我有一种预感,这是因为异常冒泡到事件处理程序(因为脚本引擎使用信号来调用我导出的类中的函数),所以我重新实现了 QApplication::notify,以在那里处理异常,但是他们没有被捕获。
我的问题是,我是否做错了根本性的事情?此外,作为替代方案,是否可以从我的 C++ 类中显式抛出脚本异常?
提前致谢
编辑:修复了包含 catch(...) 语句的描述。
更新(解决方案): 我通过遵循与接受的答案中概述的策略类似的策略“修复”了这个问题。虽然我还没有找到 linux 上没有捕获异常的原因(我现在怀疑 m_engine->evaluate 在 linux 上产生了一个单独的线程),但我已经开始使用 intended Qt 脚本中的异常抛出方式,即QScriptContext::throwError()
.
如果我的函数看起来像这样:(随机示例)
void SomeClass::doStuff(unsigned int argument) {
if (argument != 42) {
throw InvalidArgumentException(
"Not the answer to Life, the Universe and Everything.");
}
// function that is not part of the scripting environment,
// and can throw a C++ exception
dangerousFunction(argument);
}
现在是这样的:(特别注意返回类型)
QScriptValue SomeClass::doStuff(unsigned int argument) {
if (argument != 42) {
// assuming m_engine points to an instance of
// QScriptEngine that will be calling this function
return m_engine->currentContext()->throwError(QScriptContext::SyntaxError,
"Not the answer to Life, the Universe and Everything.");
}
try {
// function that is not part of the scripting environment,
// and can throw a C++ exception
dangerousFunction(argument);
} catch (ExpectedException const& e) {
return m_engine->currentContext()->throwError(QScriptContext::UnknownError,
e.message());
}
// if no errors returned, return an invalid QScriptValue,
// equivalent to void
return QScriptValue();
}
那么在哪里处理这些脚本错误呢?在调用 QScriptEngine::evaluate()
之后,您可以使用 QScriptEngine::hasUncaughtException()
检查是否有任何未捕获的异常。 , 用 uncaughtException()
获取错误对象,现在你在脚本中有发生错误的消息、跟踪和行号!
希望这对某人有所帮助!
最佳答案
在尝试将 SWIG 与 Python 结合使用来包装 C++ 库时,我遇到了类似的问题。最终发生的事情是我为所有包装类创建了一个 stub ,它捕获了异常并安静地失败了。幸运的是,我拥有仅传递容器类和状态模式对象的包装功能,因此我可以轻松检查是否有问题。我可以给你同样的建议吗?
是的,如果您允许脚本引擎访问异常工厂(一个唯一目的是抛出 C++ 异常的类),它很有可能抛出 C++ 异常。
关于c++ - 处理导出到 QtScript 的函数中抛出的 C++ 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3609940/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!