how to catch unknown exception and print it

C++Exception

C++ Problem Overview


I have some program and everytime I run it, it throws exception and I don't know how to check what exactly it throws, so my question is, is it possible to catch exception and print it (I found rows which throws exception) thanks in advance

C++ Solutions


Solution 1 - C++

If it derives from std::exception you can catch by reference:

try
{
    // code that could cause exception
}
catch (const std::exception &exc)
{
    // catch anything thrown within try block that derives from std::exception
    std::cerr << exc.what();
}

But if the exception is some class that has is not derived from std::exception, you will have to know ahead of time it's type (i.e. should you catch std::string or some_library_exception_base).

You can do a catch all:

try
{
}
catch (...)
{
}

but then you can't do anything with the exception.

Solution 2 - C++

In C++11 you have: std::current_exception

Example code from site:

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
 
void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try {
        if (eptr) {
            std::rethrow_exception(eptr);
        }
    } catch(const std::exception& e) {
        std::cout << "Caught exception \"" << e.what() << "\"\n";
    }
}
 
int main()
{
    std::exception_ptr eptr;
    try {
        std::string().at(1); // this generates an std::out_of_range
    } catch(...) {
        eptr = std::current_exception(); // capture
    }
    handle_eptr(eptr);
} // destructor for std::out_of_range called here, when the eptr is destructed

Solution 3 - C++

If you use ABI for gcc or CLANG you can know the unknown exception type. But it is non standard solution.

See here https://stackoverflow.com/a/24997351/1859469

Solution 4 - C++

Try as suggested by R Samuel Klatchko first. If that doesn't help, there's something else that might help:

a) Place a breakpoint on the exception type (handled or unhandled) if your debugger supports it.

b) On some systems, the compiler generates a call to an (undocumented?) function when a throw statement is executed. to find out, what function that is for your system, write a simple hello world program, that throws and catches an exception. start a debugger and place a breakpoint in the exceptions constructor, and see from where it is being called. the caling function is probably something like __throw(). afterwards, start the debugger again with the program you want to investigate as debuggee. place breakpoint on the function mentioned above (__throw or whatever) and run the program. when the exception is thrown, the debugger stops and you are right there to find out why.

Solution 5 - C++

Inspired by Dawid Drozd answer:

#include <exception>
try
{
    // The code that could throw
}
catch(...)
{
    auto expPtr = std::current_exception();
    
    try
    {
        if(expPtr) std::rethrow_exception(expPtr);
    }
    catch(const std::exception& e) //it would not work if you pass by value
    {
        std::cout << e.what();
    }
}

Solution 6 - C++

Inspired by hamaney answer:

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>

int main()
{
   try
   { 
      // Your code
   }
   catch (...)
   {
      try
      {
	     std::exception_ptr curr_excp;
		 if (curr_excp = std::current_exception())
		 {
            std::rethrow_exception(curr_excp);
         }
      }
	  catch (const std::exception& e)
	  {
         std::cout << e.what();
      }
   }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhelloWorldView Question on Stackoverflow
Solution 1 - C++R Samuel KlatchkoView Answer on Stackoverflow
Solution 2 - C++GelldurView Answer on Stackoverflow
Solution 3 - C++Grzegorz WolszczakView Answer on Stackoverflow
Solution 4 - C++AxelView Answer on Stackoverflow
Solution 5 - C++hamaneyView Answer on Stackoverflow
Solution 6 - C++Lyndon BautoView Answer on Stackoverflow