英文:
How to catch sdbus::Error exception in c++?
问题
我是新手学习C++。我尝试使用JNI处理在我的Java应用程序中可能发生的一些错误。这是我的try/catch块:
std::future<lib::LibVector> libVectorFuture;
try {
libVectorFuture = some::lib::getVector(param1, param2);
} catch (...) {
// 报告问题给Java。
jclass Exception = env->FindClass("com/my/MyClientException");
env->ThrowNew(Exception, "无法从本地getVector(String p1, String p2)方法获取结果!");
}
lib::LibVector vector = libVectorFuture.get();
// 这里我在使用vector
当我使用有效的参数(param1, param2)时,它可以工作。但是当我使用无效的参数时,我会得到错误:
在抛出'sdbus::Error'实例后终止调用
以及其他一些文本。此外,应用程序停止了。从我的理解中,在catch块中,我应该能够捕获任何错误,但这并没有发生。为什么?如何捕获任何错误?
英文:
I'm new in c++. I'm trying to process some errors which can occurs in my java application with jni. This is my try/catch block:
std::future<lib::LibVector> libVectorFuture;
try {
libVectorFuture = some::lib::getVector(param1, param2);
} catch (...) {
// report problem back to Java.
jclass Exception = env->FindClass("com/my/MyClientException");
env->ThrowNew(Exception, "Unable to get result from native getVector(String p1, String p2) method!");
}
lib::LibVector vector = libVectorFuture.get();
// here I'm using vector
It works when I use valid params (param1, param2). But when I'm using invalid parameters I get error:
> terminate called after throwing an instance of 'sdbus::Error'
and some other text. Also, the application stopped. As I understand in the catch block I can catch any error, but it is not happening. Why? And how to catch any error?
答案1
得分: 0
Here is the translated content:
"最后,我编写了一个解决方案。感谢您的回复。
std::futurelib::LibVector libVectorFuture;
try {
libVectorFuture = some::lib::getVector(param1, param2);
lib::LibVector vector = libVectorFuture.get(); // 这里我遇到了错误
} catch (...) {
// 将问题报告给Java。
jclass Exception = env->FindClass("com/my/MyClientException");
env->ThrowNew(Exception, "无法从本地getVector(String p1, String p2)方法获取结果!");
return nullptr;
}"
英文:
Finally, I wrote a solution. Thanks for your replies.
std::future<lib::LibVector> libVectorFuture;
try {
libVectorFuture = some::lib::getVector(param1, param2);
lib::LibVector vector = libVectorFuture.get(); // here I get error
} catch (...) {
// report problem back to Java.
jclass Exception = env->FindClass("com/my/MyClientException");
env->ThrowNew(Exception, "Unable to get result from native getVector(String p1, String p2) method!");
return nullptr;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论