Skip to content

Commit

Permalink
Catch exceptions by const reference
Browse files Browse the repository at this point in the history
This avoids the risk of slicing objects thrown as exceptions if they are
a subclass of the type we catch.
  • Loading branch information
johnkeeping committed Jun 14, 2018
1 parent 6811097 commit a35cb29
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/test-strict_fstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void test_open(const std::string& stream_class, const std::string& stream_type,
s_p->open(filename, static_cast< std::ios_base::openmode >(mode));
exception_thrown = false;
}
catch (std::exception) {}
catch (const std::exception &) {}
std::cout << stream_class << " " << stream_type << " " << (set_fail_bit? "failbit" : "nofailbit") << " "
<< strict_fstream::detail::static_method_holder::mode_to_string(
static_cast< std::ios_base::openmode >(mode))
Expand Down
2 changes: 1 addition & 1 deletion src/strict_fstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct static_method_holder
is_p->peek();
peek_failed = is_p->fail();
}
catch (std::ios_base::failure e) {}
catch (const std::ios_base::failure &e) {}
if (peek_failed)
{
throw Exception(std::string("strict_fstream: open('")
Expand Down

0 comments on commit a35cb29

Please sign in to comment.