标签:
1.异常号是有try块中抛出
// exceptions #include <iostream> using namespace std; int main () { try { throw 20; } catch (int e) //e = 20 { cout << "An exception occurred. Exception Nr. " << e << ‘\n‘; } return 0; }
try { // code here } catch (int param) { cout << "int exception"; } catch (char param) { cout << "char exception"; } catch (...) { cout << "default exception"; }
2.标准异常
#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 { throw myex; } catch (exception& e) //标准异常 { cout << e.what() << ‘\n‘; } return 0; }
#include <iostream> #include <exception> using namespace std; int main () { try { int* myarray= new int[1000]; //内存分配失败 } catch (exception& e) { cout << "Standard exception: " << e.what() << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/hinice/p/5390632.html