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 7967205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:37:58+00:00 2026-06-04T06:37:58+00:00

I have some trouble handling custom C++ exceptions when calling from Cython. My situation

  • 0

I have some trouble handling custom C++ exceptions when calling from Cython.
My situation is the following: I have a library that uses CustomLibraryException for all exceptions. What I want is basically get the error message and raise a Python error with it.

The user guide has some hints but it is a bit unspecific.
The first possibility is to do:

cdef int bar() except +ValueError

This converts the CustomLibraryException to a ValueError, but loses the error message.

The other possibility is to explicitly convert the error using

cdef int raise_py_error()
cdef int something_dangerous() except +raise_py_error

I don’t really understant this solution. I understood that raise_py_error has to be a C++ function that somehow handles the error. I am not sure how to handle it though. The function doesn’t get an argument and is called inside the catch block in C++.

If any one has an working example of handling a C++ exception in Cython, that would be of great help.

  • 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-06-04T06:37:59+00:00Added an answer on June 4, 2026 at 6:37 am

    If CustomLibraryException derives from std::runtime_error (as a well-behaved C++ exception should), then the behavior you’re seeing is a bug in Cython.

    If it doesn’t, then the easiest thing to do is to wrap the C++ function you’re calling in a C++ helper function that translates the exception:

    double foo(char const *, Bla const &);  // this is the one we're wrapping
    
    double foo_that_throws_runtime_error(char const *str, Bla const &blaref)
    {
        try {
            return foo(str, blaref);
        } catch (CustomLibraryException const &e) {
            throw std::runtime_error(e.get_the_message());
        }
    }
    

    This will cause a RuntimeError to be raised on the Python side. Alternatively, throw an std::invalid_argument to raise a ValueError, etc. (see the table in the page you linked to).

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

Sidebar

Related Questions

I have an application that's having some trouble handling multi-processor systems. It's not an
I have this open-source library that I'm having some trouble fixing a problem... This
I have some trouble achieving adequate real-time performance from my application and wondering if
I have run into some trouble with the gd library's imagefilledpolygon() . For some
I'm having some trouble with an SQL statement that have to find the number
Im having some trouble getting the syntax right. I have a movie clip that
i have some trouble to connect an link inside an dijit.dialog. Iam calling an
I have some trouble with a git repository that contains several submodules. The super
I have some trouble with pointers. I have a function that should return the
I have some trouble, saving values from input stream (cin) into an array. int

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.