7

Possible Duplicate:
Determining exception type after the exception is caught?

Following up on this question , I'd like to print out the current exception in a catch(...) block -- just for logging. One answer there says that there is no standard way of doing this, but I don't like taking no for an answer :-)

current_exception() is a function mentioned in various places on the web but apparently not well-supported. Any thoughts on this? After all, even C has errno.

Because it can be rethrown (with a simple **throw*), the exception object must be available somehow.

I am using MSVS 9.0.

Edit: The conclusion seems to be that this is not possible.

Community
  • 1
  • 1
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
  • 4
    I don't see how this question is different than the one you referenced. Just because you don't like the answer is no reason to ask again. – Mark Ransom Feb 23 '10 at 17:53
  • I agree with Mark. But, maybe you don't realize, that you can use the accepted's answer's technique to catch the base classes of all common exceptions, like catching std::exception& and MFC's which can be caught as CException*. –  Feb 23 '10 at 18:03
  • The question is a bit different: The other one asks about identifying the exception's type, whereas this one wants to print as much information as possible about the exception. – Joshua Fox Dec 02 '11 at 14:10

4 Answers4

8

If you only care about exceptions that you know about when you're writing the code then you can write a handler that can deal with all 'known' exceptions. The trick is to rethrow the exception that you caught with catch(...) and then catch the various known exceptions...

So, something like:

try
{
 ...
}
catch(...)
{
   if (!LogKnownException())
   {
      cerr << "unknown exception" << endl;
   }
}

where LogKnownException() looks something like this:

bool LogKnownException()
{
   try
   {
      throw;
   }
   catch (const CMyException1 &e)
   {
      cerr << "caught a CMyException: " << e << endl;

      return true;
   }
   catch (const Blah &e)
   {
      ...
   }
   ... etc

   return false;
}
Len Holgate
  • 21,282
  • 4
  • 45
  • 92
1

Determine what exceptions can be thrown and use a set of catch handlers to catch a set of common base types that covers them all.


As for getting the exception object from catch(...), it can't be done portably and as far as I know, it can't be done at all using the Microsoft compiler or gcc. What makes you think the exception object still exists in a catch(...) handler anyway?

JoeG
  • 12,994
  • 1
  • 38
  • 63
  • 3
    >" What makes you think the exception object still exists in a catch(...) handler anyway?" Because it can be rethrown (with a simple "throw"), the object must be there somewhere. – Joshua Fox Feb 25 '10 at 08:34
  • If the catch(...) is the top level catch handler? The as-if rule would entitle the compiler to destroy the object whenever is/was convenient. I doubt that happens in practice though. – JoeG Feb 25 '10 at 10:09
0

You can turn on RTTI and use typeOf function. current_exception is purely stl function, and applies to stl exceptions only.
As a recommendation, use different catch(exctype) per exception type. This will make life a lot easier.

alemjerus
  • 8,023
  • 3
  • 32
  • 40
  • 2
    `catch(...)` doesn't give you an object to reference, so you can't know the type. And does RTTI work on POD types, such as int? – Mark Ransom Feb 23 '10 at 17:51
0

Like alemjerus already said: current_exception works only for stl exceptions. To get various stl errors you could also write:

#include <stdexcept>
#include <exception> //ecxeption (base class)
#include <new>       //bad_alloc
#include <typeinfo>  //bad_cast und bad_typeid
#include <ios>       //ios_base::failure    

...

try
{
  ...
}
catch(std::exception& e)
{
  cerr<<"Error: "<<e.what()<<endl;
}
road242
  • 2,492
  • 22
  • 30