Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 5ddd0ea

Browse files
committed
[examples] Reduce scope of try-catch block and add missing catches
1 parent 2543afa commit 5ddd0ea

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

examples/concurrent_hash_map/concurrent_hash_map.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ main(int argc, char *argv[])
3737
pool<root> pop;
3838
bool remove_hashmap = false;
3939

40-
try {
41-
if (argc < 2)
42-
std::cerr << "usage: " << argv[0]
43-
<< " file-name [remove_hashmap]" << std::endl;
40+
if (argc < 2)
41+
std::cerr << "usage: " << argv[0]
42+
<< " file-name [remove_hashmap]" << std::endl;
4443

45-
auto path = argv[1];
44+
auto path = argv[1];
4645

47-
if (argc == 3)
48-
remove_hashmap = std::string(argv[2]) == "1";
46+
if (argc == 3)
47+
remove_hashmap = std::string(argv[2]) == "1";
4948

50-
try {
51-
pop = pool<root>::open(path, "concurrent_hash_map");
52-
} catch (pmem::pool_error &e) {
53-
std::cerr << e.what() << std::endl;
54-
return -1;
55-
}
49+
try {
50+
pop = pool<root>::open(path, "concurrent_hash_map");
51+
} catch (pmem::pool_error &e) {
52+
std::cerr << e.what() << std::endl;
53+
return -1;
54+
}
5655

56+
try {
5757
auto &r = pop.root()->pptr;
5858

5959
if (r == nullptr) {
@@ -184,16 +184,19 @@ main(int argc, char *argv[])
184184
r = nullptr;
185185
});
186186
}
187-
pop.close();
187+
} catch (const pmem::transaction_out_of_memory &e) {
188+
std::cerr << "Exception occurred: " << e.what() << std::endl;
188189
} catch (std::exception &e) {
189190
std::cerr << "Exception occurred: " << e.what() << std::endl;
190-
try {
191-
pop.close();
192-
} catch (const std::logic_error &e) {
193-
std::cerr << "Exception: " << e.what() << std::endl;
194-
}
191+
}
192+
193+
try {
194+
pop.close();
195+
} catch (const std::logic_error &e) {
196+
std::cerr << "Exception: " << e.what() << std::endl;
195197
return -1;
196198
}
199+
197200
return 0;
198201
}
199202
//! [concurrent_hash_map_ex]

examples/concurrent_hash_map/concurrent_hash_map_string.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,17 @@ main(int argc, char *argv[])
202202
r = nullptr;
203203
});
204204
}
205-
} catch (std::exception &e) {
205+
} catch (const pmem::transaction_out_of_memory &e) {
206+
std::cerr << "Exception occurred: " << e.what() << std::endl;
207+
retval = -127;
208+
} catch (const std::exception &e) {
206209
std::cerr << "Exception occurred: " << e.what() << std::endl;
207210
retval = -1;
208211
}
209212
try {
210213
pop.close();
211214
} catch (const std::logic_error &e) {
212-
std::cerr << "Exception: " << e.what() << std::endl;
215+
std::cerr << "Exception occurred: " << e.what() << std::endl;
213216
retval = -2;
214217
}
215218
return retval;

examples/pool/pool_as_class_member.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ class Foo {
4646
~Foo()
4747
{
4848
/* Close a pmemobj pool */
49-
pop.close();
49+
try {
50+
pop.close();
51+
} catch (...) {
52+
/* Let's ignore all exceptions during close */
53+
}
5054
}
5155

5256
void

examples/transaction/transaction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2016-2020, Intel Corporation */
2+
/* Copyright 2016-2021, Intel Corporation */
33

44
/*
55
* transaction.cpp -- C++ documentation snippets.
@@ -302,6 +302,9 @@ struct simple_ptr {
302302
} catch (pmem::transaction_free_error &e) {
303303
std::cerr << e.what() << std::endl;
304304
std::terminate();
305+
} catch (pmem::transaction_scope_error &e) {
306+
std::cerr << e.what() << std::endl;
307+
std::terminate();
305308
}
306309
}
307310

include/libpmemobj++/container/concurrent_hash_map.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2828,7 +2828,7 @@ class concurrent_hash_map
28282828
* [start_percent, start_percent + amount_percent]
28292829
* is incorrect.
28302830
*
2831-
* @throw rethrows pmem::defrag_error when a failure during
2831+
* @throw pmem::defrag_error rethrows defrag_error when a failure during
28322832
* defragmentation occurs. Even if this error is thrown,
28332833
* some of objects could have been relocated,
28342834
* see in such case defrag_error.result for summary stats.

0 commit comments

Comments
 (0)