酸菜猪蹄的程序人生
木下编程屯屯烫烫
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关系。
 
 
posted on 2006-01-06 17:51 cooelaf 阅读(3551) 评论(4)  编辑 收藏 引用 所属分类: Pure C/C++
Comments
  • # re: C++异常捕获机制
    小明
    Posted @ 2006-01-09 12:26
    你说:
    因为exceMaker是CDerived的友元函数,可以访问它的私有成员,故可以上溯到CBase&

    这种说法太牵强,我不能理解。

    我认为这是vc6/vs2005的C++ exception实现的一个Bug。
    你的最后一个例子 我在C++ BuilderX下面测试的结果是:
    caught else
      回复  更多评论   
  • # re: C++异常捕获机制
    cooelaf
    Posted @ 2006-01-10 08:32
    to 小明:
    我最开始的时候也是这样想,因为最后一种继承方式是私有继承,不是isa关系,所以不应该上溯到CBase,如果如你所说,可能是vs系列的bug,我会查找一下c++标准对这方面的说法。谢谢。  回复  更多评论   
  • # re: C++异常捕获机制
    yskin
    Posted @ 2006-02-22 11:04
    最后一个:
    gcc
    caught else  回复  更多评论   
  • # re: C++异常捕获机制
    cooelaf
    Posted @ 2006-02-22 16:55
    看来确实是这样。  回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理