我用的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;
    }           
}

解决方案 »

  1.   

    还有,这个在vc下面不可以正常运行,提示如下
    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;
    }
      

  2.   

    可以啊,
    #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!
      

  3.   

    哦,我没有说清楚:)我的意思是怎么捕获new抛出的异常??
      

  4.   

    通常情况下,new不抛出异常,被编译器过滤了