C++異常捕獲是在運(yùn)行時(shí)進(jìn)行的,但是拋出的對(duì)象卻是在編譯時(shí)確定的,編譯時(shí)會(huì)對(duì)拋出的對(duì)象上溯查找到無(wú)二義性的基類,然后拋出這個(gè)對(duì)象的引用。
例如:
#include <iostream>
using namespace std;
class CBase
{
public:
virtual ~CBase(){};
};
class CDerived:public CBase
{
};
void exceMaker()
{
throw CDerived();
}
void exceCatcher()
{
try
{
exceMaker();
}
catch(CBase&)
{
cout << "caught a CBase" << endl;
}
catch(...)
{
cout << "caught else" << endl;
}
}
int main(int argc, char** argv)
{
exceCatcher();
cin.get();
return 0;
}
運(yùn)行后將打印出:
caught a CBase.
編譯器進(jìn)行了類型上溯轉(zhuǎn)換,拋出的CBase的引用,如果同時(shí)捕獲CDerived&,也即在exce中加入如下代碼:
catch(CDerived&)
{
cout << "caught a CDerived" << endl;
}
編譯時(shí)將會(huì)給出warning,說(shuō)異常已經(jīng)被 catch(CBase&)捕獲,證明在編譯時(shí)進(jìn)行了轉(zhuǎn)換。
而如果修改CDerived 為私有繼承CBase,整體代碼如下:
#include <iostream>
using namespace std;
class CBase
{
public:
virtual ~CBase(){};
};
class CDerived:private CBase
{
};
void exceMaker()
{
throw CDerived();
}
void exceCatcher()
{
try
{
exceMaker();
}
catch(CBase&)
{
cout << "caught a CBase" << endl;
}
catch(...)
{
cout << "caught else" << endl;
}
}
int main(int argc, char** argv)
{
exceCatcher();
cin.get();
return 0;
}
將打印出"caught else";
因?yàn)樗接欣^承后,exceMaker函數(shù)不能對(duì)私有繼承的基類進(jìn)行上溯(private權(quán)限限制),所以拋出的異常為CDerived&,不再是CBase&.
而如果這樣:
#include <iostream>
using namespace std;
class CBase
{
public:
virtual ~CBase(){};
};
class CDerived:private CBase
{
friend void exceMaker();
};
void exceMaker()
{
throw CDerived();
}
void exceCatcher()
{
try
{
exceMaker();
}
catch(CBase&)
{
cout << "caught a CBase" << endl;
}
catch(...)
{
cout << "caught else" << endl;
}
}
int main(int argc, char** argv)
{
exceCatcher();
cin.get();
return 0;
}
在VC6中將打印出"caught CBase",因?yàn)閑xceMaker是CDerived的友元函數(shù),可以訪問(wèn)它的私有成員,故可以上溯到CBase&,但后續(xù)的編譯器版本已經(jīng)更正為caught else. 因?yàn)椴皇荌SA關(guān)系。