2929*/
3030
3131#include < cerrno> // for errno
32- #include < cstdlib> // for std::exit
3332#include < cstring> // for std::strerror
3433#include < iostream> // for std::cout, std::cerr
3534#include < string> // for std::string
@@ -73,8 +72,7 @@ class IndexFile {
7372 explicit IndexFile (const std::string& filename) :
7473 m_fd(::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666)) { // NOLINT(hicpp-signed-bitwise)
7574 if (m_fd < 0 ) {
76- std::cerr << " Can't open index file '" << filename << " ': " << std::strerror (errno) << " \n " ;
77- std::exit (2 );
75+ throw std::system_error{errno, std::system_category (), " Can't open index file '" + filename};
7876 }
7977#ifdef _WIN32
8078 _setmode (m_fd, _O_BINARY);
@@ -102,7 +100,7 @@ class IndexFile {
102100int main (int argc, char * argv[]) {
103101 if (argc != 3 ) {
104102 std::cerr << " Usage: " << argv[0 ] << " OSMFILE DIR\n " ;
105- std::exit ( 2 ) ;
103+ return 2 ;
106104 }
107105
108106 try {
@@ -117,15 +115,15 @@ int main(int argc, char* argv[]) {
117115#endif
118116 if (result == -1 && errno != EEXIST) {
119117 std::cerr << " Problem creating directory '" << output_dir << " ': " << std::strerror (errno) << " \n " ;
120- std::exit ( 2 ) ;
118+ return 2 ;
121119 }
122120
123121 // Create the output file which will contain our serialized OSM data
124122 const std::string data_file{output_dir + " /data.osm.ser" };
125123 const int data_fd = ::open (data_file.c_str (), O_WRONLY | O_CREAT | O_TRUNC, 0666 ); // NOLINT(hicpp-signed-bitwise)
126124 if (data_fd < 0 ) {
127125 std::cerr << " Can't open data file '" << data_file << " ': " << std::strerror (errno) << " \n " ;
128- std::exit ( 2 ) ;
126+ return 2 ;
129127 }
130128
131129#ifdef _WIN32
@@ -196,7 +194,7 @@ int main(int argc, char* argv[]) {
196194 } catch (const std::exception& e) {
197195 // All exceptions used by the Osmium library derive from std::exception.
198196 std::cerr << e.what () << ' \n ' ;
199- std::exit ( 1 ) ;
197+ return 1 ;
200198 }
201199}
202200
0 commit comments