error_demo.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // error_demo.cpp --------------------------------------------------------------------//
  2. // Copyright Beman Dawes 2009
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/filesystem
  6. //--------------------------------------------------------------------------------------//
  7. // //
  8. // The purpose of this program is to demonstrate how error reporting works. //
  9. // //
  10. //--------------------------------------------------------------------------------------//
  11. #include <boost/filesystem.hpp>
  12. #include <boost/system/system_error.hpp>
  13. #include <iostream>
  14. using std::cout;
  15. using boost::filesystem::path;
  16. using boost::filesystem::filesystem_error;
  17. using boost::system::error_code;
  18. using boost::system::system_error;
  19. namespace fs = boost::filesystem;
  20. namespace
  21. {
  22. void report_system_error(const system_error& ex)
  23. {
  24. cout << " threw system_error:\n"
  25. << " ex.code().value() is " << ex.code().value() << '\n'
  26. << " ex.code().category().name() is " << ex.code().category().name() << '\n'
  27. << " ex.what() is " << ex.what() << '\n'
  28. ;
  29. }
  30. void report_filesystem_error(const system_error& ex)
  31. {
  32. cout << " threw filesystem_error exception:\n"
  33. << " ex.code().value() is " << ex.code().value() << '\n'
  34. << " ex.code().category().name() is " << ex.code().category().name() << '\n'
  35. << " ex.what() is " << ex.what() << '\n'
  36. ;
  37. }
  38. void report_status(fs::file_status s)
  39. {
  40. cout << " file_status::type() is ";
  41. switch (s.type())
  42. {
  43. case fs::status_error:
  44. cout << "status_error\n"; break;
  45. case fs::file_not_found:
  46. cout << "file_not_found\n"; break;
  47. case fs::regular_file:
  48. cout << "regular_file\n"; break;
  49. case fs::directory_file:
  50. cout << "directory_file\n"; break;
  51. case fs::symlink_file:
  52. cout << "symlink_file\n"; break;
  53. case fs::block_file:
  54. cout << "block_file\n"; break;
  55. case fs::character_file:
  56. cout << "character_file\n"; break;
  57. case fs::fifo_file:
  58. cout << "fifo_file\n"; break;
  59. case fs::socket_file:
  60. cout << "socket_file\n"; break;
  61. case fs::type_unknown:
  62. cout << "type_unknown\n"; break;
  63. default:
  64. cout << "not a valid enumeration constant\n";
  65. }
  66. }
  67. void report_error_code(const error_code& ec)
  68. {
  69. cout << " ec:\n"
  70. << " value() is " << ec.value() << '\n'
  71. << " category().name() is " << ec.category().name() << '\n'
  72. << " message() is " << ec.message() << '\n'
  73. ;
  74. }
  75. bool threw_exception;
  76. }
  77. int main(int argc, char* argv[])
  78. {
  79. if (argc < 2)
  80. {
  81. cout << "Usage: error_demo path\n";
  82. return 1;
  83. }
  84. error_code ec;
  85. //// construct path - no error_code
  86. //try { path p1(argv[1]); }
  87. //catch (const system_error& ex)
  88. //{
  89. // cout << "construct path without error_code";
  90. // report_system_error(ex);
  91. //}
  92. //// construct path - with error_code
  93. path p (argv[1]);
  94. fs::file_status s;
  95. bool b (false);
  96. fs::directory_iterator di;
  97. // get status - no error_code
  98. cout << "\nstatus(\"" << p.string() << "\");\n";
  99. threw_exception = false;
  100. try { s = fs::status(p); }
  101. catch (const system_error& ex)
  102. {
  103. report_filesystem_error(ex);
  104. threw_exception = true;
  105. }
  106. if (!threw_exception)
  107. cout << " Did not throw exception\n";
  108. report_status(s);
  109. // get status - with error_code
  110. cout << "\nstatus(\"" << p.string() << "\", ec);\n";
  111. s = fs::status(p, ec);
  112. report_status(s);
  113. report_error_code(ec);
  114. // query existence - no error_code
  115. cout << "\nexists(\"" << p.string() << "\");\n";
  116. threw_exception = false;
  117. try { b = fs::exists(p); }
  118. catch (const system_error& ex)
  119. {
  120. report_filesystem_error(ex);
  121. threw_exception = true;
  122. }
  123. if (!threw_exception)
  124. {
  125. cout << " Did not throw exception\n"
  126. << " Returns: " << (b ? "true" : "false") << '\n';
  127. }
  128. // query existence - with error_code
  129. // directory_iterator - no error_code
  130. cout << "\ndirectory_iterator(\"" << p.string() << "\");\n";
  131. threw_exception = false;
  132. try { di = fs::directory_iterator(p); }
  133. catch (const system_error& ex)
  134. {
  135. report_filesystem_error(ex);
  136. threw_exception = true;
  137. }
  138. if (!threw_exception)
  139. {
  140. cout << " Did not throw exception\n"
  141. << (di == fs::directory_iterator() ? " Equal" : " Not equal")
  142. << " to the end iterator\n";
  143. }
  144. // directory_iterator - with error_code
  145. cout << "\ndirectory_iterator(\"" << p.string() << "\", ec);\n";
  146. di = fs::directory_iterator(p, ec);
  147. cout << (di == fs::directory_iterator() ? " Equal" : " Not equal")
  148. << " to the end iterator\n";
  149. report_error_code(ec);
  150. return 0;
  151. }