Boost exception at runtime
Solution 1
The following line is in error:
std::ofstream ofs("c:\test");
The compiler would've spit out a warning (at least) if your file was called jest
; but '\t'
-- being the escape for inserting a tab, your error goes by uncaught. In short, the file will not be created. You can test this with:
if (ofs.good()) { ... }
Now, since the file was not created, you don't have a valid iterator to pass on to boost::archive::text_oarchive
which throws the exception.
Try this:
std::ofstream ofs("c:\\test");
// --^ (note the extra backslash)
if (ofs.good()) {
boost::archive::text_oarchive oa(ofs);
// ...
}
Hope this helps!
PS: A final nit I couldn't stop myself from making -- if you are going to use
using namespace std;
then
ofstream ofs("c:\\test");
is good enough. Of course, it is not an error to qualify ofstream
, but it would not be the best coding style. But then, you know using using namespace
is bad, don't you?
PPS:Thank you -- sharptooth
for reminding me that \t
gets you a tab!
Solution 2
You need to catch the exception and then examine its exception_code to see what the root cause is.
Comments
-
Dan almost 2 years
Using this code:
#include <fstream> #include <boost/archive/text_oarchive.hpp> using namespace std; int main() { std::ofstream ofs("c:\test"); boost::archive::text_oarchive oa(ofs); }
I'm getting an unhandled exception at runtime on executing the boost archive line:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::archive::archive_exception> >
-
sharptooth about 15 years\t is horizontal TAB, so the compiler is very very unlikely to feel the smell.
-
Ferruccio about 15 yearsYou could also just use c:/test - the forward slash works on both Windows and Unix systems as a path separator and doesn't need to be quoted.
-
dirkgently about 15 yearsSure. Very few use it though on Windows.
-
Dan about 15 yearsThanks, I very quickly figured it out after catching the exception (please excuse my asking this question, it had been a long day), but nonetheless there are some useful tips there, so thanks. I'm curious though, why is using namespace bad?
-
dirkgently about 15 yearsThe `using namespace std' dumps all symbols in the std namespace to the global namespace causing pollution. Look up namespace pollution.