Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:43:50+00:00 2026-05-27T01:43:50+00:00

I am developing a server-client application in which the client calls a server’s API

  • 0

I am developing a server-client application in which the client calls a server’s API which gives a Python interface for user input. It means the client interface and server interface is written in Python whereas the socket code is in C++.

On the server side:-

I have a class, Test, in C++ and this class is inherited in Python named TestPython using director feature of SWIG.
Also I have an exception class MyException in C++.

Now a function of TestPython class throws MyException() from Python code.

I want to handle exception thrown from Python in C++ code using SWIG.

Below is code snippet:

C++ Code-

class MyException
{
   public:
     string errMsg;
     MyException();
     MyException(string);
     ~MyException();
};

class Test
{
    int value;
    public:
      void TestException(int val);
      Test(int);
};

Python Code –

class TestPython(Test):
   def __init__(self):
     Test.__init__(self)

   def TestException(self,val):
     if val > 20:   
       throw MyException("MyException : Value Exceeded !!!")   
     else:    
       print "Value passed = ",val

Now, if the TestException() function is called, it should throw MyException. I want to handle this MyException() exception in my C++ code.

So can anyone suggest my how to do that, I mean what should I write in my *.i(interface) file to handle this.

The above TestException() written in Python is called by the client, so I have to notify the client if any exception is thrown by the server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:43:51+00:00Added an answer on May 27, 2026 at 1:43 am

    To do this you basically need to write a %feature("director:except") that can handle a Python exception and re-throw it as a C++ one. Here’s a small but complete example:

    Suppose we have the following header file we wish to wrap:

    #include <iostream>
    #include <exception>
    
    class MyException : public std::exception {
    };
    
    class AnotherException : public std::exception {
    };
    
    class Callback {
    public:
            virtual ~Callback() { std::cout << "~Callback()" << std:: endl; }
            virtual void run() { std::cout << "Callback::run()" << std::endl; }
    };
    
    inline void call(Callback *callback) { if (callback) callback->run(); }
    

    And this Python code that uses it:

    import example 
    
    class PyCallback(example.Callback):
        def __init__(self):
            example.Callback.__init__(self)
        def run(self):
            print("PyCallback.run()")
            raise example.MyException()
    
    callback = PyCallback()
    example.call(callback)
    

    We can define the following SWIG interface file:

    %module(directors="1") example
    %{
    #include "example.h"
    %}
    
    %include "std_string.i"
    %include "std_except.i"
    %include "pyabc.i"
    
    // Python requires that anything we raise inherits from this
    %pythonabc(MyException, Exception);
    
    %feature("director:except") {
        PyObject *etype = $error;
        if (etype != NULL) {
          PyObject *obj, *trace;
          PyErr_Fetch(&etype, &obj, &trace);
          Py_DecRef(etype);
          Py_DecRef(trace);
          // Not too sure if I need to call Py_DecRef for obj
    
          void *ptr;
          int res = SWIG_ConvertPtr(obj, &ptr, SWIGTYPE_p_MyException, 0);
          if (SWIG_IsOK(res) && ptr) {
            MyException *e = reinterpret_cast< MyException * >(ptr);
            // Throw by pointer (Yucky!)
            throw e;
          }
    
          res = SWIG_ConvertPtr(obj, &ptr, SWIGTYPE_p_AnotherException, 0);
          if (SWIG_IsOK(res) && ptr) {
            AnotherException *e = reinterpret_cast< AnotherException * >(ptr);
            throw e; 
          }
    
          throw Swig::DirectorMethodException();
        }
    }
    
    %feature("director") Callback;
    %include "example.h"
    

    Which handles an error from a director call, looks to see if it was one of our MyException instances and then re-throws the pointer if it was. If you have multiple types of exception being thrown then you will probably need to use PyErr_ExceptionMatches to work out what type it is first.

    We could throw also by value or reference using:

      // Throw by value (after a copy!)
      MyException temp = *e;
      if (SWIG_IsNewObj(res)) 
        delete e;
      throw temp;
    

    instead, but note that if you threw a subclass of MyException in Python this would fall foul of the object slicing problem.

    I’m not quite sure if the code is 100% correct – in particular I think the reference counting is correct, but I could be wrong.

    Note: In order to make this example work (%pythonabc wouldn’t work otherwise) I had to call SWIG with -py3. This in turn meant I had to upgrade to SWIG 2.0, because my installed copy of Python 3.2 had removed some deprecated functions from the C-API that SWIG 1.3.40 called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing client-server application in Java which need user login. For that I
I am developing a Client-Server based application in which client application will access server
I am developing an application in C# WPF which will have Client-Server architecture (Client
I'm developing an application that uses webservices in python, both sides (server and client)
I' developing a WCF client which makes Asynchronous calls to the WCF server. I'm
I am developing a client-server application using .Net Remoting. From my server I want
I'm developing a server in C# which can accept only one client and I
I'm developing a java interface between a streaming server and a flash client. I
I'm using .NET 3.5 and WCF for developing a server-client application. Binding=BasicHttp. I'm working
I am developing a simple web application which contains these 3 components: client that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.