Standard Exceptions in C++
The built-in Standard Class Library in the C++ programming language provides its users with a hierarchy or structure library of multiple exception classes for pre-defined C++ programs. All of these are automatically derived from the base or parent class std::exception in C++ programming. These Standard Class Library exceptions are used to represent multiple types of programming errors and special programming conditions that occur during the execution process of all C++ programs.

So, let’s explore Standard Class Library exceptions in C++ programming in detail.
std::exception — Base Class Concept in C++.
In C++ programming, the std::exception class acts as the base or root class for all standard class exception types in a built-in C++ program. It provides the C++ user with a what() member function. Which returns a C-style string value indicating or representing an exception that is displayed in a class.
std::exception — syntax of the Base Class.
class exception {
public:
virtual const char* what() const noexcept;
};
Explanation std::exception of the Base Class.
- In this syntax, the what() function returns the value information describing an exception of a class. It is defined as a virtual function in the current syntax and can be overridden in a derived class to display a special, unique class error message in the current class.
- Noexcept in a class specifier indicates that this function does not throw any exceptions in the class.
Derived Exception Class Concept in C++.
The Standard Exceptions Library defines several exception classes in a C++ program, which are automatically inherited from std::exception in the current program. These exception methods display multiple types of errors in the class program, which are common methods in any C++ program.
Here are some of the most commonly used standard exception class methods in a C++ program.
std::runtime_error exception in C++.
This C++ program exception displays common program errors and issues that occur during the program execution process. Such as inputting a false value in the current C++ program, logical expression condition errors, or common issues occurring at program runtime.
The std::runtime_error exception is commonly used in a C++ program when such errors occur in a C++ program. Because of this, it cannot be corrected at runtime in the program.
std::runtime_error Constructor method.
std::runtime_error(const std::string& what_arg);
Example of a std::runtime_error exception.
#include <iostream>
#include <stdexcept> // Use this preprocessor header file for std::runtime_error exceptions
using namespace std;
void testDivision(int divisor_value) {
if (divisor_value == 0) {
throw runtime_error(“Dividing any integer value with zero is not permitted”);
}
}
int main() {
try {
testDivision(0); // In the try-catch block, this throws a runtime_error exception.
} catch (const runtime_error& e) {
cout << “Division Value Caught exception is – ” << e.what() << endl;
}
return 0;
}
Explanation of a std::runtime_error exception.
- Division Value Caught exception is – Dividing any integer value with zero is not permitted.
std::logic_error exception in C++.
In a C++ program, a std::logic_error exception represents multiple program errors generated by applying program logic expression conditions to the current class program. These logical expression errors typically reflect problems or false assumptions in the design development of C++ programs.
Examples of std::logic_error exceptions include out-of-bounds array data access, precondition violation, or invariant violation.
std::logic_error exception constructor.
std::logic_error(const std::string& what_arg);
Example of std::logic_error exception.
#include <iostream>
#include <stdexcept> // Use this preprocessor header file for std::logic_error exception
using namespace std;
void analyzeLogicExcpton(bool logic) {
if (!logic) {
throw logic_error(“display logic error, user-defined logic condition is false”);
}
}
int main() {
try {
analyzeLogicExcpton(false); // in try catch block it Throws a logic_error exception error
} catch (const logic_error& e) {
cout << “it Caught logical exception – ” << e.what() << endl;
}
return 0;
}
Explanation of std::logic_error exception.
- it Caught logical exception – display Logic error, user define logic Condition is false
std::out_of_range exception in C++.
This C++ program exception is thrown when a program attempts to access or use a vector data element value that is outside the valid range of an array, vector, or other data type container defined in the current program.
This exception is a variant of the std::logic_error out-of-range error message in the current program. It displays a signal indicating an invalid data range value access during program execution.
std::out_of_range exception constructor.
std::out_of_range(const std::string& what_arg);
Example of a std::out_of_range exception.
#include <iostream>
#include <stdexcept> // use this preprocessor header file for std::out_of_range exception
#include <vector>
using namespace std;
int main() {
vector<int> vctr = {7, 4, 5, 6};
try {
// here it Accessing an element out of bounds vector messages
cout << vctr.at(9) << endl; // in try catch block it Throws std::out_of_range vector bound exception error
} catch (const out_of_range& e) {
cout << “it Caught exception – ” << e.what() << endl;
}
return 0;
}
Explanation of std::out_of_range exception.
- it Caught exception – vector::_M_range_check: __n (which is 9) >= this->size() (which is 4)
std::invalid_argument exception in C++.
This C++ program exception is displayed in a program when a function in the current C++ program receives an invalid argument value expression. For example, passing a negative number value where you only have the possibility to pass a positive number value. This is a type of exception message similar to std::logic_error in C++ programs.
std::invalid_argument exception constructor.
std::invalid_argument(const std::string& what_arg);
Example of std::invalid_argument exception.
#include <iostream>
#include <stdexcept> // Use this preprocessor header file for std::invalid_argument exception
using namespace std;
void validatePositive(int numeric_value) {
if (numeric_value <= 0) {
throw invalid_argument(“Entered value Argument should be in positive number”);
}
}
int main() {
try {
validatePositive(-9); // in try catch block it Throws invalid_argument exception error
} catch (const invalid_argument& e) {
cout << “Caught exception – ” << e.what() << endl;
}
return 0;
}
Explanation of std::invalid_argument exception.
- Caught exception – Entered value Argument should be in positive number

