Exceptions in C++ are a language-level error-handling mechanism.
They allow you to signal that something unexpected has happened, separate normal code from error-handling code, and ensure automatic cleanup during stack unwinding.
Exceptions are not used for debugging; they are used for recovering from runtime errors or reporting unexpected conditions in a controlled way. Debugging is a separate process.
we use the exceptions using the syntax of c++98 , by overriding the what() function defined in the exception stander class .
You use exceptions by:
Throwing an exception when an error occurs:
throw MyError("failed");
Catching it where you can handle it:
catch (const std::exception& e) {
std::cout << e.what();
}
Defining custom exception types by inheriting from std::exception (recommended but not required).
You can create your own exception classes by publicly inheriting from std::exception and optionally overriding the virtual what() function.
Why override what()?
The base std::exception::what() returns a generic message: "std::exception".
Custom exceptions often need to provide more specific error information.