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

  • Home
  • SEARCH
  • 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 8980239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:02:01+00:00 2026-06-15T20:02:01+00:00

Related questions: Determining exception type after the exception is caught? How can I determine

  • 0

Related questions:

  • Determining exception type after the exception is caught?
  • How can I determine the current exception in a catch (…) block?

This question differs because I don’t care about portability. I’m interested in code specifically for g++ or perhaps even specific versions of g++ (4.6.3). It will not be used in production.

I’m dealing with legacy code that has thousands of throw statements with perhaps hundreds of thrown types. This code runs on nearly 1000 machines and catches about 40 throws per day. It is not repeatable.

At the outside layer, I can do a try { /…/ } catch (…) { /* caught it */ } and see that an exception was thrown. But I have not been able to find the type of the exception, let alone the location it is thrown from.

I believe the information must be available because code like the following works and prints “Y”:

#include <iostream>
using namespace std;
struct X {};
struct Y {};
struct Z {};
int main(int, char **) {
    try {
        //...
        throw Y();
        //...
    } catch (...) {
        cout << "Caught unknown" << endl;
        try {
            throw;
        } catch (const X &x) {
            cout << "X" << endl;
        } catch (const Y &y) {
            cout << "Y" << endl;
        } catch (const Z &z) {
            cout << "Z" << endl;
        }
    }
}

Are there any [non-portable|dirty|nasty|ugly]* tricks to identify the exception type under g++ in a catch (…)?

  • 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-15T20:02:03+00:00Added an answer on June 15, 2026 at 8:02 pm

    Here’s what I use:

    #include <cxxabi.h>
    
    using std::string;
    string deMangle(const char* const name)
    {
      int status = -1;
      char* const dem = __cxxabiv1::__cxa_demangle(name, 0, 0, &status);
    
      const string ret = status == 0 ? dem : name;
    
      if (status == 0)
        free(dem);
    
      return ret;
    }
    
    string getGenericExceptionInfo()
    {
      const std::type_info* t = __cxxabiv1::__cxa_current_exception_type();
      char const* name = t->name();
    
      return deMangle(name);
    }
    

    Usage:

    catch (...)
      {
        std::cerr << "caught: " << getGenericExceptionInfo() << std::endl;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This related question is about determining the max value of a signed type at
There are .vimrc related questions found here and here . This question wants to
Looking at the related questions, I don't think this specific question has been asked,
Two related questions After I run a program how can I see what machine
Two related questions. First: does this repro for you or is it something local
2 related questions : question #1 : I have a myTable with id ,
This is a semi-related question to question to the following question I just raised:
I saw two related questions about this topic - one has an old link
I read through the related questions and didn't find my answer. This isn't about
After reading a few related questions I've decided to use the tpl library to

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.