温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++ 异常抛出以及捕获

发布时间:2020-07-02 00:14:47 来源:网络 阅读:733 作者:CLEVERlBOY 栏目:编程语言

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

#include <iostream>
#include <string>
#include <exception>

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 <iostream>
#include <string>
#include <exception>

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这个类却又有这个构造函数。这里记得就行了。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI