0

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

I am aware of exception handling in C#:

try{
// code causing exception
}
catch(System.Exception e){
// Here e variable holds the information about the actual exception that has occured in try block.
}

But, i want to achieve samething in VC++ (on VS 2008). How do we capture the TYPE of exception that has occured in the try block in VC++, as we don't have concept of packages in VC++?

Community
  • 1
  • 1
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • Same question: http://stackoverflow.com/questions/561997/determining-exception-type-after-the-exception-is-caught Hope it helps. – jAC Dec 03 '12 at 09:47

2 Answers2

1

There is no a single base class for all exceptions in c++, so the only option, is specify what you want to handle

try
{
}
catch (const std::exception& e)
{
}
catch (const my_base_exception& e)
{
}
catch (const some_library_base_exception& e)
{
}
catch (...)
{
// ups something unknown
}

Bear in mind though, that if your my_base_exception is derived from std::exception, it will be intercepted by catch (const std::exception& e), so swap these two catches if that's the case. the same goes for some_library_base_exception

0

In C++ you generally specify the type that the catch can catch, instead of checking the type.

If you want to log the most derived type of a caught std::exception, then you can get that information via typeid, since std::exception is a polymorphic type.

If you're interested in logging types of exceptions outside of the standard exception type hierarhcy, then a good way is to catch generically (using ...) and call a common rethrower function that knows of the possible non-standard exceptions and rethrows and catches those. This centralizes the logic. But most likely your question is not about this somewhat advanced and rare technique (only relevant for use of non-well-behaved libraries), but rather, a simple misconception about exception handling in C++, for which the answer is, specify the relevant type that you're prepared to catch, in each catch clause.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331