What Kind of Exceptions are Available in C++
To handle runtime faults and extraordinary circumstances, C++ has a variety of exception types. In C++, a few of the often used exceptions are as follows:
- std::exception: The root class for all common C++ exceptions is this one. It offers a virtual method called what() that produces a string describing the exception as its result. Runtime issues, including arithmetic overflow or out-of-bounds array access, are indicated via the exception std::runtime_error.
This exception, std::logic_error, is used to highlight logical mistakes such calling a function with the wrong parameters or using an uninitialized variable.
- std::bad_alloc: When an attempt to allocate dynamic memory fails, this exception is raised.
- std::out_of_range: When an index or iterator is outside of range, an exception is raised.
- std::invalid_argument: When a function is called, this exception is raised called with an unfounded justification. When a mathematical function is called with an argument outside of its domain, the exception std::domain_error is raised.
The exception std::length_error is raised if a container is larger than it should be. The exception std::overflow_error is raised if an overflow occurs during an arithmetic operation.
- std::underflow_error: This exception is raised if an underflow occurs during an arithmetic operation. When a dynamic_cast action fails, the exception std::bad_cast is raised.
- std::bad_typeid: When typeid is used on an expression that contains a null pointer or a reference to an object that has been removed, this exception is raised.
- std::bad_function_call: When a null function pointer is called, this exception is raised.
- std::ios_base::failure: When an I/O operation goes wrong, this exception is raised.
- std::system_error: To report faults found by the operating system or other low-level system components, utilise this exception. When a regular expression cannot be built or matched, an exception called std::regex_error is raised. When an operation on a std::future or std::promise fails, this exception is thrown, and its name is std::future_error.
- std::experimental::filesystem::filesystem_error: When accessing or modifying the filesystem, this exception is issued if there is a problem.
- std::bad_weak_ptr: When a weak_ptr is used to access a deleted object, this exception is raised.
- std::out_of_memory: This exception is raised when the system's memory is exhausted.
- std::ios_base::iostate: Input/output operation problems are reported using this exception.
- std::experimental::string_view::out_of_range: When an index or iterator in a string_view object is outside of range, this exception is raised. This exception, std::chrono::system_clock::time_point, is used to indicate problems with clock-related actions.
- std::bad_function_call: When a function object is called with incorrect arguments, an exception is raised.
- std::regex_constants::error_type: To report problems with regular expression matching, use this exception. The exception std::invalid_argument is raised if an invalid argument is passed to a function. When a mathematical function is called with an argument outside of its domain, the exception std::domain_error is raised. The exception std::length_error is raised if a container is larger than it should be.
- std::underflow_error: This exception is raised if an underflow occurs during an arithmetic operation.
- std::bad_optional_access: When an optional object without a value is accessed, this exception is raised. When a regular expression cannot be built or matched, an exception called std::regex_error is raised. The exception std::overflow_error is raised if an overflow occurs during an arithmetic operation.
- std::system_error: To report faults found by the operating system or other low-level system components, utilise this exception. When there are insufficient system resources, the exception std::thread::resource_error is raised and a thread cannot be created.
- std::out_of_range: When an index or iterator is outside of range, an exception is raised. The exception std::invalid_argument is raised if an invalid argument is passed to a function. Using an uninitialized variable or invoking an undefined function are examples of logical faults that are indicated by the exception std::logic_error. Runtime issues, including arithmetic overflow or out-of-bounds array access, are indicated via the exception std::runtime_error.
- std::bad_alloc: When an attempt to allocate dynamic memory fails, this exception is raised.
Although C++ offers a range of exceptions for managing runtime mistakes and extraordinary circumstances, it's crucial to remember that it's always preferable to prevent exceptions wherever feasible by following good programming practises and creating strong code.
To maintain the stability and dependability of your programme when exceptions do arise, it's critical to manage them correctly. Other exceptions that may be used in C++ exist as well, and you can even construct your own custom exceptions by deriving from the pre-existing exception classes.