-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionClass.cpp
48 lines (38 loc) · 1.14 KB
/
ExceptionClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
class Exception
{
private:
std::string m_exception_description;
public:
Exception(std::string description) : m_exception_description { description } {}
std::string get_description() { return m_exception_description; }
};
class ArrayException : public Exception
{
private:
int m_index;
std::string m_value; // should be a template class to handle all types of values
public:
ArrayException(std::string value, int index, std::string description) : Exception(description), m_value { value }, m_index { index } {}
int get_index() { return m_index; }
std::string get_value() { return m_value; }
};
int main()
{
std::string arr[] { "1", "2", "0" };
try
{
for (int i = 0; i < 3; ++i)
{
if (arr[i] == "0")
throw ArrayException(arr[i], i, "Value cannot be equal to 0");
}
}
catch(ArrayException& exception)
{
std::cout << "[EXCEPTION] Value: " << exception.get_value() << " ; Index: " << exception.get_index() << std::endl;
std::cout << exception.get_description() << std::endl;
}
return 0;
}