Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Custom Exceptions in C++

Introduction

Exception handling in C++ is a powerful mechanism that allows a program to react to exceptional circumstances (like runtime errors) in a controlled manner. Custom exceptions help in creating more readable and maintainable code by allowing developers to define their own exceptions for specific error conditions.

Why Use Custom Exceptions?

Using custom exceptions provides several benefits:

  • Improves code readability and maintainability.
  • Allows more precise control over exception handling.
  • Enables the creation of meaningful error messages specific to the application.

Creating Custom Exceptions

To create a custom exception in C++, you need to define a new class that inherits from the std::exception class. This class should override the what() function to provide a custom error message.

#include <exception>
#include <string>

class MyCustomException : public std::exception {
private:
    std::string message;
public:
    MyCustomException(const std::string& msg) : message(msg) {}
    
    virtual const char* what() const noexcept override {
        return message.c_str();
    }
};

Throwing Custom Exceptions

To throw a custom exception, you simply create an instance of your custom exception class and use the throw keyword.

#include <iostream>

void doSomething(int value) {
    if (value < 0) {
        throw MyCustomException("Negative value not allowed!");
    }
    std::cout << "Value is: " << value << std::endl;
}

int main() {
    try {
        doSomething(-1);
    } catch (const MyCustomException& ex) {
        std::cerr << "Caught custom exception: " << ex.what() << std::endl;
    }
    return 0;
}

Catching Custom Exceptions

Custom exceptions are caught in the same way as standard exceptions. You use a try block to wrap the code that might throw an exception and a catch block to handle the exception.

Example: Full Program

Here is a complete example demonstrating the creation, throwing, and catching of a custom exception in C++:

#include <iostream>
#include <exception>
#include <string>

class MyCustomException : public std::exception {
private:
    std::string message;
public:
    MyCustomException(const std::string& msg) : message(msg) {}
    
    virtual const char* what() const noexcept override {
        return message.c_str();
    }
};

void doSomething(int value) {
    if (value < 0) {
        throw MyCustomException("Negative value not allowed!");
    }
    std::cout << "Value is: " << value << std::endl;
}

int main() {
    try {
        doSomething(-1);
    } catch (const MyCustomException& ex) {
        std::cerr << "Caught custom exception: " << ex.what() << std::endl;
    }
    return 0;
}
Output:
Caught custom exception: Negative value not allowed!

Conclusion

Custom exceptions in C++ provide a way to handle specific error conditions in a more controlled and meaningful manner. By creating your own exception classes, you can make your code more readable, maintainable, and easier to debug.