try catch throw在c++中怎么用啊?

解决方案 »

  1.   


    try
    {
       /// 需要进行异常捕获的代码
       ......
    }
    catch (CException* e)
    {
        e->ReportError();
        e->Delete();
        return;
    }
      

  2.   

    try {
       // code that could throw an exception
    }
    [ catch (exception-declaration) {
       // code that executes when exception-declaration is thrown
       // in the try block
    }
    [catch (exception-declaration) {
       // code that handles another exception type
    } ] . . . ]
    // The following syntax shows a throw expression:
    throw [expression]// exceptions_trycatchandthrowstatements2.cpp
    // compile with: /EHsc
    #include <iostream>
    using namespace std;
    void MyFunc( void );class CTest {
    public:
       CTest() {};
       ~CTest() {};
       const char *ShowReason() const { 
          return "Exception in CTest class."; 
       }
    };class CDtorDemo {
    public:
       CDtorDemo();
       ~CDtorDemo();
    };CDtorDemo::CDtorDemo() {
       cout << "Constructing CDtorDemo.\n";
    }CDtorDemo::~CDtorDemo() {
       cout << "Destructing CDtorDemo.\n";
    }void MyFunc() {
       CDtorDemo D;
       cout<< "In MyFunc(). Throwing CTest exception.\n";
       throw CTest();
    }int main() {
       cout << "In main.\n";
       try {
           cout << "In try block, calling MyFunc().\n";
           MyFunc();
       }
       catch( CTest E ) {
           cout << "In catch handler.\n";
           cout << "Caught CTest exception type: ";
           cout << E.ShowReason() << "\n";
       }
       catch( char *str )    {
           cout << "Caught some other exception: " << str << "\n";
       }
       cout << "Back in main. Execution resumes here.\n";
    }
      

  3.   

    网上资料很多的,google一下就知道了