Added a templated derived exception to KeyNotFound so that you can figure out *which* key wasn't found.

This commit is contained in:
Jesse Beder
2009-01-01 02:40:18 +00:00
parent 5a2a317d62
commit 27bd791ea0
4 changed files with 25 additions and 13 deletions

View File

@@ -79,6 +79,20 @@ namespace YAML
: RepresentationException(line_, column_, ErrorMsg::KEY_NOT_FOUND) {} : RepresentationException(line_, column_, ErrorMsg::KEY_NOT_FOUND) {}
}; };
template <typename T>
class TypedKeyNotFound: public KeyNotFound {
public:
TypedKeyNotFound(int line_, int column_, const T& key_)
: KeyNotFound(line_, column_), key(key_) {}
T key;
};
template <typename T>
TypedKeyNotFound <T> MakeTypedKeyNotFound(int line, int column, const T& key) {
return TypedKeyNotFound <T> (line, column, key);
}
class BadDereference: public RepresentationException { class BadDereference: public RepresentationException {
public: public:
BadDereference() BadDereference()

View File

@@ -119,7 +119,7 @@ namespace YAML
} }
} }
throw KeyNotFound(m_line, m_column); throw MakeTypedKeyNotFound(m_line, m_column, key);
} }
template <typename T> template <typename T>

View File

@@ -11,11 +11,13 @@ void run()
YAML::Parser parser(fin); YAML::Parser parser(fin);
YAML::Node doc; YAML::Node doc;
parser.GetNextDocument(doc); parser.GetNextDocument(doc);
for(unsigned i=0;i<doc.size();i++) {
bool value; std::cout << "name: " << doc["name"] << "\n";
doc[i] >> value; std::cout << "age: " << doc["age"] << "\n";
std::cout << (value ? "true" : "false") << "\n"; } catch(YAML::TypedKeyNotFound <std::string>& e) {
} std::cout << "Key '" << e.key << "' not found at line " << e.line+1 << ", col " << e.column+1 << "\n";
} catch(YAML::KeyNotFound& e) {
std::cout << "Key not found at line " << e.line+1 << ", col " << e.column+1 << "\n";
} catch(YAML::Exception& e) { } catch(YAML::Exception& e) {
std::cout << "Error at line " << e.line+1 << ", col " << e.column+1 << ": " << e.msg << "\n"; std::cout << "Error at line " << e.line+1 << ", col " << e.column+1 << ": " << e.msg << "\n";
} }

View File

@@ -1,7 +1,3 @@
- true name: Brett Favre
- n position: QB
- Off teams: [ Falcons, Packers, Jets ]
- YES
- on
- FALSE
- FaLse