Blog |

How to Implement Custom Exceptions in C++

How to Implement Custom Exceptions in  C++
Table of Contents
Try Rollbar Today

Proactively discover and resolve errors

Under certain conditions, custom exceptions that are not predefined in C++ may be useful to generate. In C++, any type can be caught or thrown that matches some requirements. These are that the type should have a valid copy constructor and destructor.

Custom exceptions provide relevant information about an error to the exception handling mechanism. They can be generated by creating a new class containing the attributes needed and throwing an instance of such a class, or by inheriting from std::exception and overriding the what() function.

 

Custom C++ Exceptions Example

Here’s an example on how to create and throw a custom exception in C++:

#include <iostream>
using namespace std;

class MyCustomException : public std::exception {
    public:
char * what () {
        return "Custom C++ Exception";
    }
};

int main() {
    try {
        throw MyCustomException();
    } catch (MyCustomException mce) {
        cout << "Caught MyCustomException" << endl;
        cout << mce.what();
    }
}

In the above example, a custom class MyCustomException is created which inherits all properties from the std::exception class. The what() function from std::exception is overridden, which returns a custom error message string. Therefore, whenever MyCustomException occurs, this message is displayed.

In the main() function, a try-catch block is created to throw and handle the exception. Within the try block, an object of MyCustomException is created and thrown using the throw keyword. The exception is then caught in the catch block, where the message is printed by accessing the what() function.

Running the above code produces the following output:

Caught MyCustomException
Custom C++ Exception

 

Passing Parameters to Custom C++ Exceptions

Custom C++ exceptions can include parameters to provide relevant information about the exception and customize the error message. This can help programmers to better handle the exception.

Here’s an example on how to pass parameters to custom C++ exceptions:

#include <iostream>
using namespace std;

class MyCustomException : public std::exception {
    private:
    char * message;

    public:
    MyCustomException(char * msg) : message(msg) {}
    char * what () {
        return message;
    }
};

int main() {
    try {
        throw MyCustomException("Custom C++ Exception");
    } catch (MyCustomException mce) {
        cout << "Caught MyCustomException" << endl;
        cout << mce.what();
    }
}

In the above example, a parameter msg is defined in the public constructor of the custom MyCustomException class. This parameter is mapped to the message variable defined in the class. The message variable is then used as the error message in the what() function, which is displayed when the exception is thrown.

In the main() function, within the try block, an object of MyCustomException is created with a string value passed for the message parameter. The exception is then thrown using the throw keyword and caught in the catch block. The message is then printed by accessing the what() function.

Running the above code produces the following output:

Caught MyCustomException
Custom C++ Exception

 

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing C++ errors easier than ever. Sign Up Today!

Related Posts

See all posts

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Start continuously improving your code today.

Get Started Shape