Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to throw exceptions defined in the C++ standard library?

I was wondering whether or not it is considered OK to throw exceptions that are defined in the C++ standard library, instead of creating my own class. For example, let's consider the following (stupid) function that takes one string as an argument:

#include <stdexcept> 
#include <iostream>
#include <string>

bool useless_function(const std::string& str) {
    if (str == "true")
        return true;

    else if (str == "false")
        return false;

    else
        throw std::invalid_argument("Expected argument of either true or false");
}

and then of course, we could do something like:

int main(int argc, const char** argv) {
    try {
        const bool check = useless_function("not true");
    }

    catch (std::invalid_argument& error) {
        std::cerr << error.what() << '\n';
    }

    return 0;
}

I read here that the std::stoi family of functions throw an std::invalid_exception exception when they receive an invalid argument; that's where the above idea came from.

like image 624
ra1nmaster Avatar asked Jul 14 '15 00:07

ra1nmaster


People also ask

Can you throw exceptions in C?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

Which of the following should not throw an exception?

I also know that the following cannot throw exceptions either: Destructors. Reading/writing primitive types.

Does C support exception handling?

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.

Should you use exceptions in C++?

Exceptions are preferred in modern C++ for the following reasons: An exception forces calling code to recognize an error condition and handle it. Unhandled exceptions stop program execution. An exception jumps to the point in the call stack that can handle the error.


1 Answers

Yes, it's perfectly fine to use standard exception classes for your own purposes. If they fit your situation well, go ahead (but don't hesitate to define your own class when/if no standard class fits well).

Also note that you can derive from the standard classes, so if you can add significantly greater precision or new behavior that isn't present in the standard class, you may still want to use it as a base class.

The better question (IMO) would be when it would make sense to define your own exception classes (that don't at least derive from the standard ones). One obvious candidate here would be if you want to support something like a what() that returns a string in something like UTF-16 or UTF-32 encoding, so the "stock" "std::exception" wouldn't provide much (if any) utility, and you're pretty much stuck with starting over from the beginning.

like image 149
Jerry Coffin Avatar answered Oct 14 '22 13:10

Jerry Coffin