Skip to content

Commit 26fe1b4

Browse files
committed
Add try/catch around most of main so we don't end with exception.
1 parent 63d63c6 commit 26fe1b4

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

src/osmcoastline_filter.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ int main(int argc, char* argv[]) {
9696
}
9797
}
9898

99-
// The vout object is an output stream we can write to instead of
100-
// std::cerr. Nothing is written if we are not in verbose mode.
101-
// The running time will be prepended to output lines.
102-
osmium::util::VerboseOutput vout{verbose};
103-
10499
if (output_filename.empty()) {
105100
std::cerr << "Missing -o/--output=OSMFILE option\n";
106101
std::exit(return_code_cmdline);
@@ -111,15 +106,20 @@ int main(int argc, char* argv[]) {
111106
std::exit(return_code_cmdline);
112107
}
113108

114-
osmium::io::Header header;
115-
header.set("generator", std::string{"osmcoastline_filter/"} + get_osmcoastline_version());
116-
header.add_box(osmium::Box{-180.0, -90.0, 180.0, 90.0});
109+
try {
110+
// The vout object is an output stream we can write to instead of
111+
// std::cerr. Nothing is written if we are not in verbose mode.
112+
// The running time will be prepended to output lines.
113+
osmium::util::VerboseOutput vout{verbose};
114+
115+
osmium::io::Header header;
116+
header.set("generator", std::string{"osmcoastline_filter/"} + get_osmcoastline_version());
117+
header.add_box(osmium::Box{-180.0, -90.0, 180.0, 90.0});
117118

118-
osmium::io::File infile{argv[optind]};
119+
osmium::io::File infile{argv[optind]};
119120

120-
vout << "Started osmcoastline_filter " << get_osmcoastline_long_version() << " / " << get_libosmium_version() << '\n';
121+
vout << "Started osmcoastline_filter " << get_osmcoastline_long_version() << " / " << get_libosmium_version() << '\n';
121122

122-
try {
123123
osmium::io::Writer writer{output_filename, header};
124124
auto output_it = osmium::io::make_output_iterator(writer);
125125

@@ -169,16 +169,16 @@ int main(int argc, char* argv[]) {
169169
}
170170

171171
writer.close();
172-
} catch (const osmium::io_error& e) {
173-
std::cerr << "io error: " << e.what() << "'\n";
174-
std::exit(return_code_fatal);
175-
}
176172

177-
vout << "All done.\n";
178-
osmium::MemoryUsage mem;
179-
if (mem.current() > 0) {
180-
vout << "Memory used: current: " << mem.current() << " MBytes\n"
181-
<< " peak: " << mem.peak() << " MBytes\n";
173+
vout << "All done.\n";
174+
osmium::MemoryUsage mem;
175+
if (mem.current() > 0) {
176+
vout << "Memory used: current: " << mem.current() << " MBytes\n"
177+
<< " peak: " << mem.peak() << " MBytes\n";
178+
}
179+
} catch (const std::exception& e) {
180+
std::cerr << e.what() << '\n';
181+
std::exit(return_code_fatal);
182182
}
183183
}
184184

src/osmcoastline_segments.cpp

+19-19
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ int main(int argc, char *argv[]) {
162162
std::exit(return_code_cmdline);
163163
}
164164

165-
segvec removed_segments;
166-
segvec added_segments;
167-
168165
try {
166+
segvec removed_segments;
167+
segvec added_segments;
168+
169169
InputFile file1{argv[optind]};
170170
InputFile file2{argv[optind + 1]};
171171

@@ -174,25 +174,25 @@ int main(int argc, char *argv[]) {
174174

175175
std::set_difference(m1.cbegin(), m1.cend(), m2.cbegin(), m2.cend(), std::back_inserter(removed_segments));
176176
std::set_difference(m2.cbegin(), m2.cend(), m1.cbegin(), m1.cend(), std::back_inserter(added_segments));
177-
} catch (const std::runtime_error& e) {
178-
std::cerr << e.what() << "\n";
179-
std::exit(return_code_fatal);
180-
}
181177

182-
if (dump) {
183-
std::cout << "Removed:\n";
184-
for (const auto& segment : removed_segments) {
185-
std::cout << " " << segment << "\n";
186-
}
178+
if (dump) {
179+
std::cout << "Removed:\n";
180+
for (const auto& segment : removed_segments) {
181+
std::cout << " " << segment << "\n";
182+
}
187183

188-
std::cout << "Added:\n";
189-
for (const auto& segment : added_segments) {
190-
std::cout << " " << segment << "\n";
184+
std::cout << "Added:\n";
185+
for (const auto& segment : added_segments) {
186+
std::cout << " " << segment << "\n";
187+
}
188+
} else if (!geom.empty()) {
189+
output_ogr(geom, format, removed_segments, added_segments);
191190
}
192-
} else if (!geom.empty()) {
193-
output_ogr(geom, format, removed_segments, added_segments);
194-
}
195191

196-
return (removed_segments.empty() && added_segments.empty()) ? 0 : 1;
192+
return (removed_segments.empty() && added_segments.empty()) ? 0 : 1;
193+
} catch (const std::exception& e) {
194+
std::cerr << e.what() << '\n';
195+
std::exit(return_code_fatal);
196+
}
197197
}
198198

src/osmcoastline_ways.cpp

+22-17
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,32 @@ int main(int argc, char* argv[]) {
129129

130130
CPLSetConfigOption("OGR_SQLITE_SYNCHRONOUS", "OFF");
131131

132-
const std::string input_osm_filename{argv[1]};
132+
try {
133+
const std::string input_osm_filename{argv[1]};
133134

134-
std::string output_db_filename{"coastline-ways.db"};
135-
if (argc >= 3) {
136-
output_db_filename = argv[2];
137-
}
135+
std::string output_db_filename{"coastline-ways.db"};
136+
if (argc >= 3) {
137+
output_db_filename = argv[2];
138+
}
138139

139-
index_type index_pos;
140-
index_type index_neg;
141-
location_handler_type location_handler{index_pos, index_neg};
140+
index_type index_pos;
141+
index_type index_neg;
142+
location_handler_type location_handler{index_pos, index_neg};
142143

143-
osmium::io::File infile{input_osm_filename};
144-
osmium::io::Reader reader1{infile, osmium::osm_entity_bits::node};
145-
osmium::apply(reader1, location_handler);
146-
reader1.close();
144+
osmium::io::File infile{input_osm_filename};
145+
osmium::io::Reader reader1{infile, osmium::osm_entity_bits::node};
146+
osmium::apply(reader1, location_handler);
147+
reader1.close();
147148

148-
CoastlineWaysHandler coastline_ways_handler{output_db_filename};
149-
osmium::io::Reader reader2{infile, osmium::osm_entity_bits::way};
150-
osmium::apply(reader2, location_handler, coastline_ways_handler);
151-
reader2.close();
149+
CoastlineWaysHandler coastline_ways_handler{output_db_filename};
150+
osmium::io::Reader reader2{infile, osmium::osm_entity_bits::way};
151+
osmium::apply(reader2, location_handler, coastline_ways_handler);
152+
reader2.close();
152153

153-
std::cerr << "Sum of way lengths: " << std::fixed << (coastline_ways_handler.sum_length() / 1000) << "km\n";
154+
std::cerr << "Sum of way lengths: " << std::fixed << (coastline_ways_handler.sum_length() / 1000) << "km\n";
155+
} catch (const std::exception& e) {
156+
std::cerr << e.what() << '\n';
157+
std::exit(return_code_fatal);
158+
}
154159
}
155160

0 commit comments

Comments
 (0)