c++ try catch exception

来源:互联网 发布:淘宝企业店铺经营类型 编辑:程序博客网 时间:2024/06/03 21:56



#include <iostream>#include <exception>using namespace std;class myexception: public exception{virtual const char* what() const throw(){return "My exception happened";}}myex;int main () {try{if(true)throw myex;}catch (exception& e){cout << e.what() << endl;}return 0;}

输出:

pateo@pateo-B86N53X:~/work/study$ g++ main.cc -o mainpateo@pateo-B86N53X:~/work/study$ ./mainMy exception happenedpateo@pateo-B86N53X:~/work/study$ 


原创粉丝点击