Hello. I have inherited a codebase which uses cv::FileNode
. There is some code that wraps the >>
operator in a try-catch block, but as far as I am able to tell this operator doesn’t throw an exception; instead it seems to store either a default constructed or garbage value.
Is there a standard way to detect or predict failure of this operator? My current thought is to write my own utility function to compare the node’s type against the variable’s type, but that seems very roundabout.
that API is terribly old, and terribly terrible too.
It’s not for arbitrary XML files, only for those written by, or compatible to, cv::FileStorage
. just saying, because someone previously thought they could use that for some arbitrary XML
I just browsed the docs. there are methods equivalent to >>
but those return nothing and the docs don’t say they’d throw anything either. the FileStorage and FileNode classes don’t appear to store any errors either, that you’d have to check for.
In some of the examples, I see them calling node.empty()
so maybe that’s how you’re expected to handle this. IDK. also there’s isOpened()
but I have no idea if that just covers opening the file or also reading it.
I don’t even know if anyone has defined a schema for that XML. if there is a schema, you could use some other library to validate your XML file.
Thanks, I appreciate the second set of eyes to confirm my interpretation. I did also dig into the code, and AFAICT it will generate an error message but doesn’t actually do anything that could notify the caller of the problem. We can check node.empty()
to ensure that the node exists, but that doesn’t tell us if it is (for example) convertible to int
.
Fortunately I don’t believe we are ever ingesting files that weren’t written by cv::FileStorage
, which is probably part of why this hasn’t really been a problem (its not as though it was associated with any specific complaint that I’m aware of).