In this tutorial, we'll be covering how to create your own custom exception classes in C++. These are extremely useful when you want to handle specific scenarios that aren't covered by standard exceptions. Exception handling is a key part of robust software design, and custom exceptions allow you to be more precise and expressive in handling errors.
By the end of this tutorial, you should be able to:
- Understand what custom exceptions are and when to use them
- Create your own custom exception classes
- Use these classes effectively in your code
Prerequisites: Basic knowledge of C++ programming, including classes and exception handling.
Custom exceptions in C++ are created by defining new types of classes. These classes are derived from the standard exception classes in C++.
Here is a simple step-by-step guide on how to create a custom exception:
std::exception
.class MyException : public std::exception {
// class definition goes here
};
what()
function is a virtual function in base exception
class in C++ Standard library. It is used to fetch the exception occurred. You should override this in your custom class.class MyException : public std::exception {
public:
const char * what () const throw () {
return "My exception happened";
}
};
Now you can throw and catch MyException
just like any other exception.
Let's see some examples on how to create and use custom exceptions:
#include <iostream>
#include <exception>
// Define the custom exception
class MyException : public std::exception {
public:
const char * what () const throw () {
return "MyException occurred";
}
};
int main() {
try {
throw MyException();
} catch(MyException& e) {
std::cout << e.what() << '\n';
} catch(std::exception& e) {
// Other errors
}
}
In this example, we throw a MyException
from within a try
block. We then catch this exception in a catch
block and call the what()
method to print the error message.
It’s often useful to be able to include some additional data in your exception classes, such as an error code.
#include <iostream>
#include <exception>
// Define the custom exception
class MyException : public std::exception {
public:
MyException(int errorCode) : m_errorCode(errorCode) {}
const char* what() const throw() {
if (m_errorCode == 1) {
return "Error code 1 occurred";
} else {
return "Error code 2 occurred";
}
}
private:
int m_errorCode;
};
int main() {
try {
throw MyException(1);
} catch(MyException& e) {
std::cout << e.what() << '\n';
} catch(std::exception& e) {
// Other errors
}
}
In this tutorial, we covered how to create custom exceptions in C++, including how to include additional data in your exceptions. These tools can be a powerful way to handle errors in your code.
For further learning, try creating your own custom exceptions in your projects, or modifying existing code to use custom exceptions.
Exercise 1: Create a custom exception class that includes additional information about the exception (like an error code or a detailed message).
Exercise 2: Write a function that throws this exception under some condition, then write a main function that calls this function in a try block and catches the exception in a catch block.
Exercise 1: Solution can be similar to the second example above.
Exercise 2: Solution will depend on the specifics of the function and condition you chose. Make sure to include a throw
statement in your function, and a catch
block in your main function that catches your custom exception.