Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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;
}
share|improve this question

closed as off topic by gnat, Walter, Nemanja Trifunovic, Glenn Nelson, MichaelT Feb 16 '13 at 2:28

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
How is this off-topic? –  James Feb 16 '13 at 2:30
add comment

2 Answers

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.

share|improve this answer
1  
They can't handle Unicode, whereas my direct guess would be that he's using VS and wants to deal with Unicode. –  DeadMG Feb 16 '13 at 0:47
add comment

Not particularly. Exception handling is already an expensive routine, so it's difficult to cock things up from that perspective, and the language doesn't treat thrown objects notably different to any other. In fact, you could simply throw a string if you wanted to. But you should design the more derived classes to contain more information, IMO.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.