when I run the following code,it just generate "aborted",but the
anticipated answer is
"Caught \"char *\" exception" ,it seems to be no run the 'catch'
sentence.
please help!
#include <iostream>
using namespace std;
class CanThrowException
{
int *array;
public:
CanThrowException(int array_size) throw (char *) {
// if ((array = (int *)malloc(sizeof(int) * array_size)) == 0)
/* If allocation fails */
throw "Memory allocation failed";
}
//... Other stuff
};
int
main()
{
CanThrowException *ins;
try { /* Try to construct object */
ins = new CanThrowException(10);
}
catch (char *) { /* Catch any exception thrown by constructor */
cerr << "Caught \"char *\" exception" << endl;
}
// catch (...) { /* If constructor can throw anything else */
// cerr << "Caught unknown exception" << endl;
// }
}