C++异常捕获是在运行时进行的,但是抛出的对象却是在编译时确定的,编译时会对抛出的对象上溯查找到无二义性的基类,然后抛出这个对象的引用。
 
例如:
#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;
}
 
运行后将打印出:
 
caught a CBase.
 
编译器进行了类型上溯转换,抛出的CBase的引用,如果同时捕获CDerived&,也即在exce中加入如下代码:
catch(CDerived&)
{
    cout << "caught a CDerived" << endl;
}
编译时将会给出warning,说异常已经被 catch(CBase&)捕获,证明在编译时进行了转换。
 
而如果修改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";
因为私有继承后,exceMaker函数不能对私有继承的基类进行上溯(private权限限制),所以抛出的异常为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",因为exceMaker是CDerived的友元函数,可以访问它的私有成员,故可以上溯到CBase&,但后续的编译器版本已经更正为caught else. 因为不是ISA关系。