exception handling class c++

  1. Create a custom exception class by deriving from the standard exception class:
class MyException : public std::exception {
public:
    const char* what() const throw() {
        return "My custom exception";
    }
};
  1. Use the custom exception class in the code by throwing it when needed:
void someFunction() {
    throw MyException();
}
  1. Handle the custom exception using a try-catch block:
try {
    someFunction();
} catch (MyException& e) {
    std::cout << "Caught my custom exception: " << e.what() << std::endl;
}