Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Exceptions with message

I'm not sure that my custom exception approach is correct. What I want to do is to throw exceptions with custom messages but it seems that I created a memory leak...

class LoadException: public std::exception {
private:
    const char* message;
public:
    LoadException(const std::string message);
    virtual const char* what() const throw();
};


LoadException::LoadException(const std::string message) {
    char* characters = new char[message.size() + 1];
    std::copy(message.begin(), message.end(), characters);
    characters[message.size()] = '\0';
    this->message = characters;
}

I use it as follows:

void array_type_guard(Local<Value> obj, const std::string path) {
    if (!obj->IsArray()) {
        throw LoadException(path + " is not an array");
    }
}

try {
    objects = load_objects();
} catch (std::exception& e) {
    ThrowException(Exception::TypeError(String::New(e.what())));
    return scope.Close(Undefined());
}

I afraid that the array created in constructor is never deleted. But I'm not sure how to delete it - should I add destructor or maybe use completely different approach?

Update:

I've actually tried to use the string class as follows:

class LoadException: public std::exception {
private:
    const char* msg;
public:
    LoadException(const std::string message);
    virtual const char* what() const throw();
};

LoadException::LoadException(const std::string message) {
    msg = message.c_str();
}

const char* LoadException::what() const throw() {
    return msg;
}

But cannot get the error message then - some random output is displayed when I print the "what()".

like image 537
Alex Netkachov Avatar asked Jul 03 '13 02:07

Alex Netkachov


People also ask

How do I show exception messages in C++?

C++ exception handling is built upon three keywords: try, catch, and throw. throw − A program throws an exception when a problem shows up. This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem.

Can C handle exceptions?

C does not provide direct support for error handling (also known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions.

Does C have Throw?

C doesn't support exception handling. To throw an exception in C, you need to use something platform specific such as Win32's structured exception handling -- but to give any help with that, we'll need to know the platform you care about. ...and don't use Win32 structured exception handling.

Does C language have exceptions?

C does not have exception handling facilities. Errors are handled by examining the value returned by each function and signals (conditions reported to the program) are handled by using library functions.


2 Answers

How about
throw std::runtime_error("My very own message");

like image 182
MonoThreaded Avatar answered Oct 19 '22 10:10

MonoThreaded


You can take advantage of std:string

class LoadException: public std::exception {
private:
    std::string message_;
public:
    explicit LoadException(const std::string& message);
    const char* what() const noexcept override {
        return message_.c_str();
    }
};


LoadException::LoadException(const std::string& message) : message_(message) {
    
}

Then the C++ scoping will take care of cleaning things up for you

like image 25
NG. Avatar answered Oct 19 '22 09:10

NG.