如何在C++中捕获sdbus::Error异常?

huangapple go评论82阅读模式
英文:

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&lt;lib::LibVector&gt; libVectorFuture;
      
        	try {
              libVectorFuture = some::lib::getVector(param1, param2);
          } catch (...) {
              // report problem back to Java.
              jclass Exception = env-&gt;FindClass(&quot;com/my/MyClientException&quot;);
              env-&gt;ThrowNew(Exception, &quot;Unable to get result from native getVector(String p1, String p2) method!&quot;);
          }

lib::LibVector vector = libVectorFuture.get();

// here I&#39;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&lt;lib::LibVector&gt; 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-&gt;FindClass(&quot;com/my/MyClientException&quot;);
          env-&gt;ThrowNew(Exception, &quot;Unable to get result from native getVector(String p1, String p2) method!&quot;);
       return nullptr;
      }

huangapple
  • 本文由 发表于 2020年8月3日 22:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定