Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exception classes are in the standard C++ library

What are the exception classes that are included in the standard C++ library, and what should they be used for? I know there are a few new C++11 exceptions, but I'm not sure what they are or where they are.

like image 207
Mooing Duck Avatar asked Aug 13 '12 17:08

Mooing Duck


People also ask

Where are standard exception classes?

Where are standard exception classes grouped? Explanation: As these are standard exceptions, they need to be defined in the standard block, So it is defined under namespace std.

What library is exception in?

The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called std::exception and is defined in the <exception> header.

How many types of standard exceptions are in C++?

There are nine standard exceptions in c++. They are bad_alloc, bad_cast, bad_exception, bad_function_call, bad_typeid, bad_weak_ptr, ios_base::failure, logic_error and runtime_error.


1 Answers

std::exception <exception> interface (debatable if you should catch this)     std::bad_alloc <new> failure to allocate storage         std::bad_array_new_length <new> invalid array length     std::bad_cast <typeinfo> execution of an invalid dynamic-cast     std::bad_exception <exception> signifies an incorrect exception was thrown     std::bad_function_call <functional> thrown by "null" std::function     std::bad_typeid <typeinfo> using typeinfo on a null pointer     std::bad_weak_ptr <memory> constructing a shared_ptr from a bad weak_ptr     std::logic_error <stdexcept> errors detectable before the program executes         std::domain_error <stdexcept> parameter outside the valid range         std::future_error <future> violated a std::promise/std::future condition         std::invalid_argument <stdexcept> invalid argument         std::length_error <stdexcept> length exceeds its maximum allowable size         std::out_of_range <stdexcept> argument value not in its expected range     std::runtime_error <stdexcept> errors detectable when the program executes         std::overflow_error <stdexcept> arithmetic overflow error.         std::underflow_error <stdexcept> arithmetic underflow error.         std::range_error <stdexcept> range errors in internal computations         std::regex_error <regex> errors from the regular expression library.         std::system_error <system_error> from operating system or other C API             std::ios_base::failure <ios> Input or output error 

Source: http://en.cppreference.com/w/cpp/error/exception
In practice, most exceptions are custom exceptions derived from logic_error and runtime_error. Not that these are neglected, but that many exceptions are domain specific.

Keep in mind that an exception should reflect what went wrong and not who threw it. (No "MyProgramException"s)

like image 186
Mooing Duck Avatar answered Oct 02 '22 17:10

Mooing Duck