关键字noexcept c中通过throw关键字抛出异常通过try{}catch{}捕获异常在对应函数后添加throw()指定可以抛出的异常类型#includeiostream using namespace std; struct MyException { MyException(string str) : msg(str) {} string msg; }; void func() throw(int ,double,MyException){ // throw(int, double, MyException)该函数只允许抛出指定类型的异常 windows中只警告但是linux中报错 // 如果throw() 括号中什么都没有就是这个函数不允许抛出异常 // 不写throw(...)是指这个函数可以抛出任意类型的异常 try { //throw 1; throw MyException(string exception!); } catch (int e) { cout throw value: e endl; } catch (MyException e) { cout Myexpection: e.msg endl; } } int main() { func(); system(pause); return 0; }就如上面代码中的例子func()函数中抛出异常并且捕获func被限制只能抛出int,double,MyException类型的异常。throw(int, double, MyException)该函数只允许抛出指定类型的异常 在windows中只警告但是linux中报错如果throw()括号中什么都没有就是这个函数不允许抛出异常不写throw(...)是指这个函数可以抛出任意类型的异常C11新增了noexcept关键字#includeiostream using namespace std; struct MyException { MyException(string str) : msg(str) {} string msg; }; void func() noexcept(true){ // 在c11中使用noexcept代替throw()表示这个函数不允许抛出异常需要抛出异常只需要去掉关键字noexcept即可 // noexcept()关键字后面可以加括号里面写表达式如果表达式返回值为truenoexcept就有效该函数不能向外抛异常返回值为falsenoexcept就无效该函数就可以向外抛异常 //throw 1; throw MyException(string exception!); } int main() { try { func(); } catch (int e) { cout throw value: e endl; } catch (MyException e) { cout Myexpection: e.msg endl; } system(pause); return 0; }