资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++异常抛出以及捕获

临近离职,决定补一下之前一直没来得及学的C++11的知识,突然翻到了异常处理,感觉有点好玩就自己写了个测试程序,然后三观彻底被颠覆了。
源代码如下:

成都创新互联公司是一家专业提供兴业企业网站建设,专注与成都网站建设、网站设计、HTML5建站、小程序制作等业务。10年已为兴业众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

#include 
#include 
#include 

void speak(int i)
{
    if(i <= 0)
    {
        throw "System get a wrong...";
    }
}

void main()
{
    try
    {
        speak(-1);
    }
    catch(std::exception e)
    {
        std::cerr << "exception info:" << e.what() << std::endl;
    }
}

很简单对不对,编译也没错,然后运行就挂了,通过Debug跟踪,发现是这么一行挂了
catch(std::exception e),然后各种怀疑,各种改,就差重装系统了。

无意中改了这么一句,然后自己把异常处理了,改后的代码如下:

#include 
#include 
#include 

void speak(int i)
{
    if(i <= 0)
    {
        throw std::exception("System get a wrong...");
    }
}

void main()
{
    try
    {
        speak(-1);
    }
    catch(std::exception e)
    {
        std::cerr << "exception info:" << e.what() << std::endl;
    }
}

注意throw 那一块,很懵,可能是exception 是一个explict的类吧,没太大的功夫研究,记住就行了。程序之所以挂掉,可能是因为这里没能捕获,直接交给操作系统了。

==================补充说明===================
上午说的那个exception类的声明,我贴在下面,我在不同的平台下又遇到问题了:

_STDEXT_BEGIN
class exception
    {   // base of all library exceptions
public:

    static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew)
        {   // register a handler for _Raise calls
        const _STD _Prhand _Pold = _STD _Raise_handler;
        _STD _Raise_handler = _Pnew;
        return (_Pold);
        }

    ====这里就是为啥char *无法自动转换为exception的原因了
    // this constructor is necessary to compile 
    // successfully header new for _HAS_EXCEPTIONS==0 scenario
    explicit __CLR_OR_THIS_CALL exception(const char *_Message = _MESG("unknown"), int x=1)
        _THROW0()
        : _Ptr(_Message)
        {   // construct from message string
                (void)x;
        }

    __CLR_OR_THIS_CALL exception(const exception& _Right) _THROW0()
        : _Ptr(_Right._Ptr)
        {   // construct by copying _Right
        }

    exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) _THROW0()
        {   // assign _Right
        _Ptr = _Right._Ptr;
        return (*this);
        }

    virtual __CLR_OR_THIS_CALL ~exception()
        {   // destroy the object
        }

    virtual const char * __CLR_OR_THIS_CALL what() const _THROW0()
        {   // return pointer to message string
        return (_Ptr != 0 ? _Ptr : "unknown exception");
        }

    void __CLR_OR_THIS_CALL _Raise() const
        {   // raise the exception
        if (_STD _Raise_handler != 0)
            (*_STD _Raise_handler)(*this);  // call raise handler if present

        _Doraise(); // call the protected virtual
        _RAISE(*this);  // raise this exception
        }

protected:
    virtual void __CLR_OR_THIS_CALL _Doraise() const
        {   // perform class-specific exception handling
        }

protected:
    const char *_Ptr;   // the message pointer
    };
_STDEXT_END

我遇到的另一个问题是在gunc上,exception并没有带char 的构造函数,要想通过char 构造exception,就只能使用有这个构造函数的子类;但是在windows平台上,exception这个类却又有这个构造函数。这里记得就行了。


分享题目:C++异常抛出以及捕获
当前URL:http://cdkjz.cn/article/jesgpi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220