Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If I wanted to implement my own version of the std::exception for no good reason

ID: 654277 • Letter: I

Question

If I wanted to implement my own version of the std::exception for no good reason, are there any special things about implementing this kind of object that I should be aware of? It seems like a fairly simple thing that is mostly a string wrapper...

Here is my implementation so far:

#pragma once;

#include <string>

class MyException
{
public:
    MyException(const std::wstring&);
    virtual ~MyException(){}

    virtual MyException& operator=(const MyException& rhs);
    virtual const std::wstring& what(){return msg;};

private:
    std::wstring msg;
};

MyException::MyException(const std::wstring& message)
:msg(message)
{
}

MyException& MyException::operator=(const MyException& rhs)
{
    if(this == &rhs)
    {
        return *this;
    }
    msg = rhs.msg;
    return *this;
}

Explanation / Answer

The best advice I could give you is to base your code on the standard exception classes. It will make your code simpler and will allow you and your users to interact with existing code, the standard library or third party libraries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote