Я знаю, что прошло некоторое время, так как это обсуждение началось. Но эта конструкция try-and-catch, упомянутая Адамом, является частью стандарта C ++ и поддерживается Microsoft VC ++ и GNU C ++. Вот программа, которая работает. Кстати, улов автоматически генерирует другое исключение, чтобы сигнализировать о сбое конструктора.
#include <iostream>
#include <exception>
#include <string>
using namespace std;
class my_exception: public exception
{
string message;
public:
my_exception(const char* message1)
{
message = message1;
}
virtual const char* what() const throw()
{
cout << message << endl;
return message.c_str();
}
virtual ~my_exception() throw() {};
};
class E
{
public:
E(const char* message) { throw my_exception(message);}
};
class A
{
E p;
public:
A()
try :p("E failure")
{
cout << "A constructor" << endl;
}
catch (const exception& ex)
{
cout << "Inside A. Constructor failure: " << ex.what() << endl;
}
};
int main()
{
try
{
A z;
}
catch (const exception& ex)
{
cout << "In main. Constructor failure: " << ex.what() << endl;
}
return 0;
}