C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, exception handling provides a systematic way to deal with unexpected situations or errors that may occur during program execution. The standard library provides some exception classes, but you can also create your own custom exception classes to handle specific errors more effectively.
In this tutorial, we'll cover how to create and use custom exception classes in C++.
std::exception
class, which is part of the C++ standard library. You can override the what()
function to provide a custom error message.Example:
#include <iostream> #include <exception> class CustomException : public std::exception { public: const char* what() const noexcept override { return "Custom exception occurred!"; } };
throw
keyword followed by an object of your custom exception class. Use try
and catch
blocks to handle the exception.Example:
#include <iostream> #include <exception> class CustomException : public std::exception { public: const char* what() const noexcept override { return "Custom exception occurred!"; } }; void functionThatThrows() { throw CustomException(); } int main() { try { functionThatThrows(); } catch (const CustomException& e) { std::cout << "Caught exception: " << e.what() << std::endl; } catch (...) { std::cout << "Caught unknown exception." << std::endl; } return 0; }
Output:
Caught exception: Custom exception occurred!
Example:
#include <iostream> #include <exception> #include <string> class CustomException : public std::exception { private: int errorCode; std::string errorMessage; public: CustomException(int code, const std::string& message) : errorCode(code), errorMessage(message) {} int getCode() const { return errorCode; } const char* what() const noexcept override { return errorMessage.c_str(); } }; void functionThatThrows() { throw CustomException(404, "Resource not found"); } int main() { try { functionThatThrows(); } catch (const CustomException& e) { std::cout << "Caught exception (code: " << e.getCode() << "): " << e.what() << std::endl; } catch (...) { std::cout << "Caught unknown exception." << std::endl; } return 0; }
Output:
Caught exception (code: 404): Resource not found
By using custom exception classes in your C++ programs, you can handle errors in a more structured and meaningful way. This can lead to more robust and maintainable code, making it easier to debug and understand error conditions.
Base class for standard exceptions in C++:
std::exception
class.#include <iostream> #include <exception> int main() { try { throw std::exception(); // Throwing an instance of std::exception } catch (const std::exception& e) { std::cerr << "Caught exception: " << e.what() << std::endl; } return 0; }
Derived classes of the exception class in C++:
std::exception
, providing specific exception types for different error scenarios.#include <iostream> #include <stdexcept> int main() { try { throw std::runtime_error("Custom runtime error"); // Throwing a derived exception } catch (const std::exception& e) { std::cerr << "Caught exception: " << e.what() << std::endl; } return 0; }
C++ custom exception class example:
std::exception
.#include <iostream> #include <stdexcept> class CustomException : public std::exception { public: const char* what() const noexcept override { return "Custom exception: Something went wrong."; } }; int main() { try { throw CustomException(); // Throwing a custom exception } catch (const std::exception& e) { std::cerr << "Caught exception: " << e.what() << std::endl; } return 0; }