Решено: Исключение в виде наследника std::exception

Модератор: Модераторы разделов

Аватара пользователя
Zeus
Сообщения: 694

Решено: Исключение в виде наследника std::exception

Сообщение Zeus »

Код: Выделить всё

#include <exception>
#include <iostream>

class myException: public std::exception
{
        public:
        myException (const std::string& __what__): _what_ (__what__)
            {
        };

        virtual    ~myException () throw ()
        {
        };

        virtual const char* what ()
        {
            return _what_.c_str();
        };

        private:
        std::string _what_;
};

class A
{
    public:
    A ()
    {
    };

    void a ()
    {
        throw myException ("A::a()");
    };
};

int
main ()
{
    try
    {
    A a;
    a.a ();
    }
    catch (std::exception& e)
//    catch (myException& e)
    {
    std::cout << e.what() << std::endl;
    };

    return 0;
};


Этот код вместо "A::a()" выводит "11myException".

Я как-то не так понимаю исключения?
Спасибо сказали:
Аватара пользователя
Zeus
Сообщения: 694

Re: Решено: Исключение в виде наследника std::exception

Сообщение Zeus »

Отбой!
Ошибка была здесь:

virtual const char* what () const throw()
{
return _what_.c_str();
};
Спасибо сказали: