Atul
Atul

Reputation: 4320

Handling exception in function returning std::string

How to handle std::bad_alloc exception in this function:

std::string GetString()
{
    std::string str;
    return str;
}

Since any stl constructor can throw bad_alloc, we've to do like this:

std::string GetString()
{
    try
    {
        std::string str;
        return str;
    }
    catch(std::bad_alloc&)
    {
        return ""; // Constructs temporary std::string and returns. Could throw !
    }
}

Again catch block is still not safe.

I just want to make this function exception proof.

Upvotes: 1

Views: 807

Answers (4)

MikeMB
MikeMB

Reputation: 21156

Although, (i think) it is not guaranteed by the standard, most (maybe all) std::string implementations don't allocate memory for an empty/short string (as mentioned by @BoBTFish). So your solution is as "exception proof", as it gets. I'd just suggest, to actually return a default constructed string instead of "".

However, the basic question you should ask yourself is if bad_alloc is something that you expect because you are potentially trying to construct a very big string or if it actually indicates, that your system completely ran out of memory:

If allocation fails, because you try to create a string of a few million characters, then constructing a shorter/empty error string will probably not thorw another exception an can be seen as proper form of error handling.

If it fails, because your program/system ran completely out of memory, you cannot deal with it locally (you e.g. cannot free any other memory) and therefore you probably also should not try to hide that error, because it will certainly come up again shortly after. So while returning an empty or short string would probably still work, I see no reason to catch that exception inside your function in the first place.

Upvotes: 2

Wolf
Wolf

Reputation: 10238

Never catch exceptions that are not semantically related to the implementation of your function. Else you will end in catching std::exception in every function, which is obviously nonsense.

I just want to make this function exception proof.

It's impossible to create a std::string and to guarantee not to throw bad_alloc.

Upvotes: 0

Paolo Brandoli
Paolo Brandoli

Reputation: 4750

As a general rule, never catch an exception unless you know what to do with it.

Also, a program that crashes because of a non-caught exception is easier to debug than a program that crashes after it tries to resolve an exception

In the case you shown, if creating an empty string is a cause for bad_alloc then also returning "" will probably cause another bad_alloc (another empty string is implicitly created).

There are very few situations where you can resolve a bad_alloc: for instance if you know that you are caching a lot of data then you can free the cached data and retry the allocation.

Upvotes: 0

Richard Critten
Richard Critten

Reputation: 2145

Try to follow this guideline: Only catch an exception if you can deal with its fundamental cause. Suppressing the exception is not solving the underlying problem that the exception has exposed.

To answer your question; there is nothing your function can do about the reason std::bad_alloc was thrown. Another function higher up the call stack might be able to do something, if you suppress the exception you make this impossible.

Upvotes: 1

Related Questions