我用的vc6.0,怎么catch由new产生的异常呢?一定要用_set_new_handler吗?
#include <iostream>using namespace std;void main()
{
try {
for (;;) {
char *p = new char[100000];
/*
if (NULL == p) {
cerr << "bad!" << endl;
break;
}
*/
}
}
catch(...) {
cerr << "I catch it!" << endl;
}
}
#include <iostream>using namespace std;void main()
{
try {
for (;;) {
char *p = new char[100000];
/*
if (NULL == p) {
cerr << "bad!" << endl;
break;
}
*/
}
}
catch(...) {
cerr << "I catch it!" << endl;
}
}
Assertion failed: new_p == 0, file setnewh.cpp, line 52abnormal program termination//////////////////////////////////////////
#include <iostream>
#include <new>
using namespace std;void out_of_store(){
cerr << "operator new failed: out of store\n";
throw bad_alloc();
}int main()
{
set_new_handler(out_of_store);
try {
for (;;) {
char *p = new char[100000];
}
}
catch (bad_alloc) {
cerr << "not enough memory\n";
}
return 0;
}
#include <iostream>using namespace std;void main()
{
try {
int i=0;
int j=0;
j = 100/i;
}
catch(...) {
cerr << "I catch it!" << endl;
}
}
// 输出:
// I catch it!