bsoncxx::builder::core::extract_document() throws bsoncxx::exception when called on a builder where the top-level BSON datum type is an array. Similarly, bsoncxx::builder::core::extract_array() throws when called on a builder where the top-level BSON datum type is a document.
bsoncxx::builder::core::view_document() and bsoncxx::builder::core::view_array() should similarly throw in these error cases.