Javatpoint Logo
Javatpoint Logo

Exception::what() in C++

Robust C++ programs typically include exception handling. During the execution of a program, when errors or exceptional conditions happen, C++ enables users to treat them in a beautiful way by utilizing try, throw, and catch statements. One essential component of this mechanism is the what() function, which is a part of the std::exception class. In this blog post, we'll delve into the exception::what() function, how it is implemented, and give illustrative examples.

Role of std::exception:

Before we dive into what() function, let's briefly discuss the std::exception class. Standard catch class for all C++ exceptions that works as a template for the creation of user-customized exceptional objects.

## Instruction: Convert the given sentence from AI written to human written std::exception has a member function called what() that produces a C-style string indicating the type of exception.

What is the what() Function?

At its core, what() function serves the primary function of furnishing a human-readable description of the transpiring exception. This description proves invaluable for logging, debugging, or presenting error messages to end-users.

Syntax:

The what() function is declared as follows:

Breaking down the function signature:

  • virtual: Denotes that the function can be overridden by derived classes.
  • const char*: The return type, a pointer to a constant character, encapsulating a C-style string.
  • const noexcept: Specifiers indicating that the function abstains from throwing exceptions.

Implementation and Overriding:

Overriding becomes imperative in custom exception classes to harness the power of what() function. Consider a scenario where a custom exception class named CustomException is created, inheriting from std::exception:

In this example, the what() function of CustomException is overridden to furnish a tailored error message for instances of this exception.

Utilizing Exception::what():

Now, let's explore the practical application of exception::what() within a try-catch block:

Output:

Caught an exception: Custom exception: Something went wrong!

Explanation:

Here, an instance of CustomException is thrown within the try block. The catch block captures the exception by reference to std::exception and outputs the result of what() to the standard error stream.

Real-world Application:

Consider a more pragmatic scenario where exception handling is applied to a function responsible for dividing two numbers. In the event of attempting to divide by zero, a custom exception, DivideByZeroException, is triggered:

Example:

Output:

Caught an exception: Division by zero error!

Conclusion:

In summary, the provided C++ code serves as a compelling illustration of the crucial role played by exception handling in gracefully managing unexpected errors. The custom exception class, "DivideByZeroException", extends the standard "std::exception" class, showcasing the utility of user-defined exceptions. The overridden "what()" function within the "DivideByZeroException" class contributes a detailed error message, elevating the comprehensibility of the exception-handling process.

Within the main function, a division operation is encapsulated within a try block. In the event of encountering a division by zero, the custom exception is strategically thrown. The ensuing catch block adeptly captures the exception, and the program emits a lucid and informative error message by utilizing "e.what()".

This exemplar underscores the significance of tailoring exception classes to specific scenarios and leveraging the "what()" function to convey precise information during runtime errors. Such practices facilitate effective debugging and enhance the communication of error details to developers and end-users. As demonstrated in this code snippet, exception handling emerges as an indispensable tool for creating resilient and comprehensible C++ programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA