Suppose I have two inheritance hierarchies that I deal with in C ++. One inherits from std::exception(the new hierarchy), and the other inherits from Exception(the base exception class of the C ++ Builder VCL class). If I call code that can throw any type of exception, I must write code like this:
try {
Function1();
Function2();
} catch (std::exception &ex) {
std::cout << "STL exception caught: " << ex.what() << std::endl;
} catch (Exception &ex) {
std::cout << "Legacy exception caught: " << ex.Message.c_str() << std::endl;
} catch (SomeOtherVendorLibraryException &ex) {
}
The problem is that every caller must have all these catch tricks in order to try to get every last type of exception, since C ++ does not have a single true, forced base exception class that you can use as catch-all (e.g. , System.Exceptionclass in C #). (It catch (...)βs not a starter, since you donβt have a way to find out what you caught, and some dangerous system exceptions, such as access violations, can also be caught that are better left unchecked).
"" std::exception. . ,.NET Framework (, COMException). - :
class LegacyException : public std::runtime_error {
public:
LegacyException(const Exception &ex) : std::runtime_error(ex.Message.c_str()) {}
};
try {
throw Exception("Throwing legacy exception!");
} catch (std::exception &ex) {
std::cout << ex.what() << std::endl;
}
, - , .
, ?