Custom Exception Class
Creating Custom Exception Classes in C++
C++ Custom Exception Class Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <exception>
#include <string>
using namespace std;
// Custom exception class
class MyException : public exception {
private:
string message;
public:
MyException(const string& msg) : message(msg) {}
const char* what() const noexcept override {
return message.c_str();
}
};
class AgeException : public exception {
private:
int age;
public:
AgeException(int a) : age(a) {}
const char* what() const noexcept override {
if (age < 0) {
return "Age cannot be negative";
} else if (age > 150) {
return "Age is unrealistic";
}
return "Invalid age";
}
int getAge() const { return age; }
};
void checkAge(int age) {
if (age < 0 || age > 150) {
throw AgeException(age);
}
cout << "Valid age: " << age << endl;
}
int main() {
try {
checkAge(25);
checkAge(-5);
} catch (const AgeException& e) {
cout << "AgeException caught: " << e.what() << endl;
}
try {
throw MyException("This is a custom exception");
} catch (const MyException& e) {
cout << "\nCustom exception: " << e.what() << endl;
}
return 0;
}Valid age: 25 AgeException caught: Age cannot be negative Custom exception: This is a custom exception
Understanding Custom Exception Class
This program teaches you how to create Custom Exception Classes in C++. Custom exceptions allow you to define domain-specific error types with custom data and behavior. They provide more meaningful error information than standard exceptions.
---
1. What This Program Does
The program demonstrates custom exception classes:
Custom exceptions enable domain-specific error handling.
---
2. Header Files Used
---
3. Understanding Custom Exceptions
Custom Exception Concept
:
Benefits
:
---
4. Creating Custom Exception
Basic Structure
:
private:
string message;
public:
const char* what() const noexcept override {
}
};
return message.c_str();How it works
:
---
5. Overriding what() Method
Implementation
:
const char* what() const noexcept override {
}
return message.c_str();How it works
:
---
6. Custom Exception with Data
Adding Data Members
:
private:
int age;
public:
const char* what() const noexcept override {
}
int getAge() const { return age; }
};
// Return message based on ageHow it works
:
---
7. Using Custom Exceptions
Throwing Custom Exception
:
if (age < 0 || age > 150) {
throw AgeException(age);
}
Catching Custom Exception
:
catch (const AgeException& e) {
}
---
cout << "AgeException: " << e.what() << endl;
cout << "Age value: " << e.getAge() << endl;8. When to Use Custom Exceptions
Best For
:
Example Scenarios
:
---
9. Important Considerations
Inheritance
:
RAII Principles
:
Error Messages
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning advanced exception design, understanding custom error types, and preparing for domain-specific error handling in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Custom Exception Class
This C++ program is part of the "Exception Handling Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.