Search code examples
javac++error-handlingjava-native-interfacetry-catch

How to catch sdbus::Error exception in c++?


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?


Solution

  • 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;
          }