看自己编写的下面一段代码
#include <string>
#include <iostream>
#include <stdexcept>
#include <exception>
#include <new>
//#include <type_info>
using namespace std;
int main()
{
int a = 0;
try{
if(a == 0)
throw exception();
if(a == 1)
throw bad_alloc();
if(a == 2)
throw bad_cast();
}
catch (exception e){ cout << e.what() << endl;}
catch (bad_alloc e){ cout << e.what() << endl;}
catch (bad_cast e){cout << e.what() << endl;}
return 9;
}
g++编译器,编译时候会提示警告:
3.cc:21:1: warning: exception of type ‘std::bad_alloc’ will be caught
catch (bad_alloc e){ cout << e.what() << endl;}
^~~~~
3.cc:20:1: warning: by earlier handler for ‘std::exception’
catch (exception e){ cout << e.what() << endl;}
自己稍微查了下,发现catch语句块必须得安排下顺序,不能随意乱安排。所以自己改了下:
把 catch (exception e){}放在最后。在c++primer书上也说过,越是具体的异常类型处理,越是得放在前面。越是通用的异常,越该放后面。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)