tookkke 2016-06-18 04:54 采纳率: 100%
浏览 1146
已采纳

c++异常处理的一件非常奇怪的事

functexcept中有一个函数__throw_out_of_range 只有声明,找不到它的定义
而且特别奇怪

 #include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
    try
    {
        __throw_out_of_range("out_of_range");
    }
    catch(out_of_range &err)
    {
        cout<<typeid(err).name()<<endl;
    }
    return 0;
}

我这样写的话,当然会编译错误,因为没#include <stdexcept>
会得到[Error] 'out_of_range' does not name a type
因为out_of_range是在stdexcept中定义的一个类,也就是我的程序中没有它的定义
但是

 #include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
    try
    {
        __throw_out_of_range("out_of_range");
    }
    catch(exception &err)
    {
        cout<<typeid(err).name()<<endl;
    }
    return 0;
}

我这样的话会输出St12out_of_range
也就是我确实是捕获到它了,所以很奇怪,怎么能捕获到一个没有声明过的对象呢
__throw_out_of_range这个函数太奇怪了

  • 写回答

3条回答 默认 最新

  • Valtava 2016-06-18 06:25
    关注

    C++文件是分别编译的,最后链接成整体程序。所以想要实现这种效果很容易啊,只需要在定义__throw_out_of_range的地方包含 stdexcept就可以了。


    这里给你个例子:

     // my_throw.hh
    
     #ifndef _MY_THROW_HH_
     #define _MY_THROW_HH_
    
     void my_throw(const char* info);
    
     #endif
    
     // my_throw.cc
    
     #include <stdexcept>
     #include "my_throw.hh"
    
     void my_throw(const char* info)
     {
         throw std::out_of_range(info);
    }
    
     //main.cc
    
    #include <iostream>
    #include <typeinfo>
    #include "my_throw.hh"
    
    using namespace std;
    
    int main()
    {
        try
        {
            my_throw("out_of_range");
        }
        catch(exception &err)
        {
            cout<<typeid(err).name()<<endl;
        }
        return 0;
    }
    

    你可以跑跑这段代码试试,应该也是正常的,而且main.cc里面(即使包含了my_thorw头文件)见不到std::out_of_range的定义或者声明。
    这里__throw_out_of_range可能已经编译器内置了,连包含都不需要

    PS:
    既然__throw_out_of_range是以两个下划线开始的,就暗示了你不该在自己的程序里使用这个函数 ... ...

     // ISO 9899:2011
    
     Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.
    
    — All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
    
    — All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效