Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost::filesystem getting error info

42 views
Skip to first unread message

Christopher J. Pisz

unread,
Nov 8, 2016, 10:46:14 PM11/8/16
to
Was looking at boost::filesystem to replace some Windows specific stuff
I have.

It isn't very clear on what what wrong if you catch a filesystem exception.

For example, calling boost::filesystem::create_directories("?!#garbage?!#);

throws an exception where, afaict the only data available is the path
argument. It has a what(), but it is an empty string.

Am I missing some manner of enlightening information?

Öö Tiib

unread,
Nov 9, 2016, 5:10:54 AM11/9/16
to
May be you miss that information from its documentation:
http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#filesystem_error-members

May be you miss way how to reach such information (I used Google
search).
If that information does not help you then please be more specific.

Öö Tiib

unread,
Nov 9, 2016, 5:16:20 AM11/9/16
to
On Wednesday, 9 November 2016 12:10:54 UTC+2, Öö Tiib wrote:
> On Wednesday, 9 November 2016 05:46:14 UTC+2, Christopher J. Pisz wrote:
> > Was looking at boost::filesystem to replace some Windows specific stuff
> > I have.
> >
> > It isn't very clear on what what wrong if you catch a filesystem exception.
> >
> > For example, calling boost::filesystem::create_directories("?!#garbage?!#);
> >
> > throws an exception where, afaict the only data available is the path
> > argument. It has a what(), but it is an empty string.
> >
> > Am I missing some manner of enlightening information?
>
> May be you miss that information from its documentation:
> http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#filesystem_error-members

On second thought it can also be that you don't know that information:
http://en.cppreference.com/w/cpp/error/system_error/code

Christopher J. Pisz

unread,
Nov 9, 2016, 10:06:07 AM11/9/16
to
In this example:

try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from the
exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes mapped to
the boost codes?
// The actual numeric value can be found in the header with the
Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your own
meaningful error message?
const boost::system::error_code errorCode = e.code();

std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with error
code: " << errorCode.message();


// Use our own exception type
throw Common::Exception(__FILE__, __LINE__, msg.str());
}


I see the members, but I get values 123. Where is a table to lookup what
123 means? If I dig through the headers, it takes me to errno.h on
Windows and points to ENOPROTOOPT, which makes no sense.

"The filename, directory name, or volume label syntax is incorrect" is a
meaningful string, but it could be alot better. I am going to want to
make a switch, handle specific codes, and write custom messages, but the
documentation is not telling me where to look up the the codes.




Öö Tiib

unread,
Nov 9, 2016, 3:01:17 PM11/9/16
to
That example does not compile as C++. Top level try?

>
>
> I see the members, but I get values 123. Where is a table to lookup what
> 123 means? If I dig through the headers, it takes me to errno.h on
> Windows and points to ENOPROTOOPT, which makes no sense.
>
> "The filename, directory name, or volume label syntax is incorrect" is a
> meaningful string, but it could be alot better. I am going to want to
> make a switch, handle specific codes, and write custom messages, but the
> documentation is not telling me where to look up the the codes.

I thought that error codes from boost are those:
http://www.boost.org/doc/libs/1_61_0/libs/system/doc/reference.html#Header-error_code

Value 123 looks overly large but it iss unsure how you got it. If you
can't post real code then perhaps use debugger or something?

Christopher J. Pisz

unread,
Nov 9, 2016, 3:45:18 PM11/9/16
to
On 11/9/2016 2:01 PM, Öö Tiib wrote:

> That example does not compile as C++. Top level try?


#include <sstream>
#include <boost/filesystem.hpp>

//--------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from
the exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes
mapped to the boost codes?
// The actual numeric value can be found in the header with
the Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your
own meaningful error message?
const boost::system::error_code errorCode = e.code();

std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with
error code: " << errorCode.message();

// Use our own exception type
throw std::runtime_error(msg.str());
}

return 0;
That's what I thought looking through the header, but "no socket option"
makes no sense.

It appears to be these codes
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx

but I want to make sure. IT doesn't mention it in the docs at all.


Öö Tiib

unread,
Nov 10, 2016, 2:02:00 AM11/10/16
to
Yep, it seems weakly documented so I started to wonder why it works
in my projects as platform agnostic. :D Appears that it depends on error
category and different error codes of different categories may be
converted or may compare equal. My bots found such mock/hacking tool
from internet somewhere:

#include <iostream>
#include <boost/asio/error.hpp>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>

int main()
{
// Two different error codes.
boost::system::error_code code1 = make_error_code(
boost::system::errc::no_such_file_or_directory);
boost::system::error_code code2 = make_error_code(
boost::asio::error::host_not_found_try_again);

// That have different error categories.
assert(code1.category() != code2.category());
assert(code1.default_error_condition().category() !=
code2.default_error_condition().category());

// Yet have the same value.
assert(code1.value() == code2.value());
assert(code1.default_error_condition().value() ==
code2.default_error_condition().value());

// Use the comparision operation to check both value
// and category.
assert(code1 != code2);
assert(code1.default_error_condition() !=
code2.default_error_condition());

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test with Boost.Filesytem
try
{
boost::filesystem::canonical("bogus_file");
}
catch(boost::filesystem::filesystem_error& error)
{
if (error.code() ==
make_error_code(boost::system::errc::no_such_file_or_directory))
{
std::cout << "No file or directory" << std::endl;
}
if (error.code() ==
make_error_code(boost::asio::error::host_not_found_try_again))
{
std::cout << "Host not found" << std::endl;
}
}
}
0 new messages