Exception Handling in OOPS
Published by
sanya sanya
Introduction:
Exception handling is a powerful feature that allows programmers to handle and manage unexpected or erroneous situations in their code. When an exceptional condition occurs during program execution, such as division by zero or attempting to access an invalid memory location, the program can throw an exception. Exception handling provides a mechanism to capture these exceptions and handle them, preventing the program from crashing or causing undefined behavior.
- Throw: The ‘throw’ keyword is used to generate an exception when a particular condition is met. It allows you to raise an exception explicitly, signaling that an error or exceptional situation has occurred.
- Try: The ‘try’ block is used to enclose the code that might potentially throw an exception. When an exception is thrown inside the ‘try’ block, it is caught by the nearest matching ‘catch’ block.
- Catch: The ‘catch’ block is used to handle exceptions. It catches the thrown exception and executes the code within the ‘catch’ block, which is responsible for handling the exceptional situation.
Types
- Standard Exceptions: These are exceptions that are part of the C++ Standard Library. They are predefined exception classes that are available for general use. Some common standard exception classes are:
- ‘exception’: The base class for all standard exceptions.
- ‘runtime_error’: Represents errors that can occur during runtime, like invalid operations or arithmetic exceptions.
- ‘logic_error’: Represents logical errors, like trying to access elements outside the bounds of an array.
- User-Defined Exceptions: Apart from standard exceptions, C++ allows programmers to define their own custom exception classes based on specific application requirements. This gives developers the flexibility to create custom exception types that convey meaningful information about the exceptional situation.
User defined
To create a user-defined exception class in C++, you typically define a new class that inherits from ‘exception’ or one of its derived classes. You can add additional data members and methods to the custom exception class to store and retrieve specific information about the exception.
Example:
#include
#include
using namespace std;
class MyException : public exception {
private:
string errorMsg;
public:
MyException(const string& msg) : errorMsg(msg) {}
// Override the what() method to provide a custom error message
const char* what() const noexcept override {
return errorMsg.c_str();
}
};
int main() {
try {
int num = -1;
if (num < 0) {
throw MyException("Negative numbers not allowed.");
}
} catch (const MyException& e) {
cout << "Exception caught: " << e.what() << endl;
}
return 0;
}
In this example, we create a custom exception class ‘MyException’ that inherits from ‘exception’. The constructor of ‘MyException’ takes an error message as a parameter and stores it in the ‘errorMsg’ data member. We override the ‘what()’ method (from ‘exception’) to provide a custom error message when the exception is caught and handled.
When the condition ‘num < 0’ is met, the ‘throw’ statement is executed, and a ‘MyException’ object is thrown. The ‘catch’ block catches this exception, and the custom error message is displayed.
Library
WEB DEVELOPMENT
FAANG QUESTIONS