I'm having trouble understanding the syntax of exceptions. I have a class template map<T>
that has to be able to throw an exception. The code bellow is correct and is used to catch the exception.
try
{
map<int> m;
m["everything"] = 42;
}
catch(map<int>::Uninitialized&)
{
cout << "Uninitialized map element!" << endl;
}
I was attempting to create a class derived from runtime_error and then throw it from my class. But it seems that my logic is flawed.
class Uninitialized : public runtime_error
{
public:
Uninitialized() : runtime_error("error") {}
};
T operator[] (const char index[]) const
{
throw Uninitialized();
return value;
}