YouTip LogoYouTip

Cpp Libs Stdexcept

`` is a header file in the C++ standard library that defines a set of standard exception classes for handling runtime errors in programs. Exceptions are errors that occur during program execution. They can be caught and handled to prevent abnormal program termination. The `` header file defines a set of exception classes derived from `std::exception`, providing a standard way to report and handle errors. ### Syntax To use the exception classes in ``, you first need to include this header file: #include Then, you can use these exception classes to throw and catch exceptions. For example: throw std::runtime_error("An error occurred"); ### Exception Classes The `` header file defines the following exception classes: * `std::exception`: The base class for all standard exception classes. * `std::bad_exception`: Thrown when an error occurs during exception handling. * `std::bad_alloc`: Thrown when memory allocation fails. * `std::bad_cast`: Thrown when type casting fails. * `std::bad_typeid`: Thrown when the `typeid` operation fails. * `std::logic_error`: Thrown when a logic error occurs, such as invalid input parameters. * `std::domain_error`: Thrown when a function argument is outside the valid range. * `std::invalid_argument`: Thrown when a function argument is invalid. * `std::length_error`: Thrown when a container operation fails due to length limitations. * `std::out_of_range`: Thrown when accessing an illegal index of a container. * `std::overflow_error`: Thrown when an arithmetic operation causes overflow. * `std::range_error`: Thrown when a function return value is outside the expected range. * `std::underflow_error`: Thrown when an arithmetic operation causes underflow. ## Example Here is a simple example using ``, demonstrating how to throw and catch exceptions: ## Example #include #include void riskyFunction(int x){ if(x <0){ throw std::invalid_argument("Negative value not allowed"); } std::cout<<"Processing "<< x << std::endl; } int main(){ try{ riskyFunction(-1); }catch(const std::invalid_argument& e){ std::cerr<<"Caught an exception: "<< e.what()<< std::endl; } return 0; } Output: Caught an exception: Negative value not allowed In this example, the `riskyFunction` function checks whether the passed argument is negative, and if so, throws a `std::invalid_argument` exception. In the `main` function, we use a `try-catch` block to catch and handle this exception. * * * ## Standard Exception Classes ### std::exception std::exception is the base class for all standard exception classes. It provides a virtual function what() that returns a string describing the exception. ## Example #include #include int main(){ try{ throw std::exception(); }catch(const std::exception& e){ std::cout<<"Caught exception: "<< e.what()<< std::endl; } return 0; } ### std::logic_error std::logic_error is derived from std::exception and represents a program logic error. It is an abstract base class, usually concretized by derived classes. ## Example #include #include int main(){ try{ throw std::logic_error("Logic error occurred"); }catch(const std::logic_error& e){ std::cout<<"Caught logic error: "<< e.what()<< std::endl; } return 0; } ### std::invalid_argument std::invalid_argument is derived from std::logic_error and indicates that an invalid argument was passed. ## Example #include #include int main(){ try{ throw std::invalid_argument("Invalid argument provided"); }catch(const std::invalid_argument& e){ std::cout<<"Caught invalid argument: "<< e.what()<< std::endl; } return 0; } ### std::domain_error std::domain_error is derived from std::logic_error and indicates that an argument is outside the valid range. ## Example #include #include int main(){ try{ throw std::domain_error("Domain error occurred"); }catch(const std::domain_error& e){ std::cout<<"Caught domain error: "<< e.what()<< std::endl; } return 0; } ### std::length_error std::length_error is derived from std::logic_error and indicates a length error, such as exceeding the maximum length limit in container operations. ## Example #include #include int main(){ try{ throw std::length_error("Length error occurred"); }catch(const std::length_error& e){ std::cout<<"Caught length error: "<< e.what()<< std::endl; } return 0; } ### std::out_of_range std::out_of_range is derived from std::logic_error and indicates that an access is outside the valid range. ## Example #include #include int main(){ try{ throw std::out_of_range("Out of range error occurred"); }catch(const std::out_of_range& e){ std::cout<<"Caught out of range error: "<< e.what()<< std::endl; } return 0; } ### std::runtime_error std::runtime_error is derived from std::exception and represents a program runtime error. It is an abstract base class, usually concretized by derived classes. ## Example #include #include int main(){ try{ throw std::runtime_error("Runtime error occurred"); }catch(const std::runtime_error& e){ std::cout<<"Caught runtime error: "<< e.what()<< std::endl; } return 0; } ### std::range_error std::range_error is derived from std::runtime_error and indicates that a calculation result is outside the representable range. ## Example #include #include int main(){ try{ throw std::range_error("Range error occurred"); }catch(const std::range_error& e){ std::cout<<"Caught range error: "<< e.what()<< std::endl; } return 0; } ### std::overflow_error std::overflow_error is derived from std::runtime_error and indicates an arithmetic overflow error. ## Example #include #include int main(){ try{ throw std::overflow_error("Overflow error occurred"); }catch(const std::overflow_error& e){ std::cout<<"Caught overflow error: "<< e.what()<< std::endl; } return 0; } ### std::underflow_error std::underflow_error is derived from std::runtime_error and indicates an arithmetic underflow error. ## Example #include #include int main(){ try{ throw std::underflow_error("Underflow error occurred"); }catch(const std::underflow_error& e){ std::cout<<"Caught underflow error: "<< e.what()<< std::endl; } return 0; } ### Using Custom Exception Classes In addition to standard exception classes, we can also define our own exception classes, inheriting from standard exception classes. ## Example #include #include class MyException :public std::runtime_error{ public: MyException(const std::string& message): std::runtime_error(message){} }; int main(){ try{ throw MyException("My custom exception occurred"); }catch(const MyException& e){ std::cout<<"Caught custom exception: "<< e.what()<< std::endl; } return 0; } `` is an important component of the C++ standard library. It provides a set of standard exception classes, making error handling more consistent and predictable. By using these exception classes, you can write more robust and maintainable code. For beginners, understanding the basic concepts of exceptions and how to use the exception classes in `` is very important.
← Cpp Libs CstdintCpp Libs Typeinfo β†’