c++ 的异常处理

 

std::bad_alloc& e 相当于python的execpt Exption  as e

 

#include <iostream>

using namespace std;


void abc(){

  throw "adfadf";

}


int main(int argc, char const *argv []){

    try {
      abc();
    }catch(bad_alloc& e){
      printf("%s\n", e.what());
    }
    return 0;
}

 

例子二

#include <iostream>
using namespace std;
int main(){
    try{
        throw "sdf";
    }catch(int intError){
        cout << intError <<'\n';
    }catch(...){
        cout << "未知错误" <<'\n';
    }
    cout <<"hello world!!!" <<'\n';
    return 0;

}

 

posted @ 2018-06-11 10:23  我当道士那儿些年  阅读(122)  评论(0编辑  收藏  举报