Skip to content

Commit

Permalink
Catch all exceptions (where possible) as const references
Browse files Browse the repository at this point in the history
A few catch blocks modify something in their exceptions, so those are kept non-const.
  • Loading branch information
Vultraz committed May 13, 2018
1 parent 60b1124 commit 0bf09c8
Show file tree
Hide file tree
Showing 101 changed files with 323 additions and 323 deletions.
6 changes: 3 additions & 3 deletions src/actions/attack.cpp
Expand Up @@ -1202,14 +1202,14 @@ bool attack::perform_hit(bool attacker_turn, statistics::attack_context& stats)
if(hits) {
try {
fire_event(attacker_turn ? "attacker_hits" : "defender_hits");
} catch(attack_end_exception) {
} catch(const attack_end_exception&) {
refresh_bc();
return false;
}
} else {
try {
fire_event(attacker_turn ? "attacker_misses" : "defender_misses");
} catch(attack_end_exception) {
} catch(const attack_end_exception&) {
refresh_bc();
return false;
}
Expand Down Expand Up @@ -1434,7 +1434,7 @@ void attack::perform()

try {
fire_event("attack");
} catch(attack_end_exception) {
} catch(const attack_end_exception&) {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/actions/undo.cpp
Expand Up @@ -298,11 +298,11 @@ void undo_list::read(const config & cfg)
if ( action ) {
undos_.push_back(action);
}
} catch (bad_lexical_cast &) {
} catch (const bad_lexical_cast &) {
ERR_NG << "Error when parsing undo list from config: bad lexical cast." << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
ERR_NG << "Skipping this undo action..." << std::endl;
} catch (config::error& e) {
} catch (const config::error& e) {
ERR_NG << "Error when parsing undo list from config: " << e.what() << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
ERR_NG << "Skipping this undo action..." << std::endl;
Expand All @@ -313,11 +313,11 @@ void undo_list::read(const config & cfg)
for (const config & child : cfg.child_range("redo")) {
try {
redos_.push_back(new config(child));
} catch (bad_lexical_cast &) {
} catch (const bad_lexical_cast &) {
ERR_NG << "Error when parsing redo list from config: bad lexical cast." << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
ERR_NG << "Skipping this redo action..." << std::endl;
} catch (config::error& e) {
} catch (const config::error& e) {
ERR_NG << "Error when parsing redo list from config: " << e.what() << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
ERR_NG << "Skipping this redo action..." << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/addon/client.cpp
Expand Up @@ -148,7 +148,7 @@ bool addons_client::upload_addon(const std::string& id, std::string& response_me
config addon_data;
try {
archive_addon(id, addon_data);
} catch(utf8::invalid_utf8_exception&){
} catch(const utf8::invalid_utf8_exception&){
this->last_error_ =
VGETTEXT("The add-on <i>$addon_title</i> has a file or directory "
"containing invalid characters and cannot be published.", i18n_symbols);
Expand Down
4 changes: 2 additions & 2 deletions src/addon/manager_ui.cpp
Expand Up @@ -95,7 +95,7 @@ bool addons_manager_ui(const std::string& remote_address)

gui2::show_error_message(
VGETTEXT("A local file with add-on publishing information could not be read.\n\nFile: $path\nError message: $msg", symbols));
} catch(wml_exception& e) {
} catch(const wml_exception& e) {
e.show();
} catch(const addons_client::user_exit&) {
LOG_AC << "initial connection canceled by user\n";
Expand Down Expand Up @@ -306,7 +306,7 @@ bool ad_hoc_addon_fetch_session(const std::vector<std::string>& addon_ids)

gui2::show_error_message(
VGETTEXT("A local file with add-on publishing information could not be read.\n\nFile: $path\nError message: $msg", symbols));
} catch(wml_exception& e) {
} catch(const wml_exception& e) {
e.show();
} catch(const addons_client::user_exit&) {
LOG_AC << "initial connection canceled by user\n";
Expand Down
2 changes: 1 addition & 1 deletion src/ai/actions.cpp
Expand Up @@ -96,7 +96,7 @@ void action_result::execute()
if (is_success()){
try {
do_execute();
} catch (return_to_play_side_exception&) {
} catch (const return_to_play_side_exception&) {
if (!is_ok()) { DBG_AI_ACTIONS << "Return value of AI ACTION was not checked." << std::endl; } //Demotes to DBG "unchecked result" warning
throw;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/composite/component.cpp
Expand Up @@ -160,7 +160,7 @@ static component *find_component(component *root, const std::string &path, path_
} else {
try {
pe.position = std::stoi(position);
} catch (std::invalid_argument&) {
} catch (const std::invalid_argument&) {
pe.position = -2;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/composite/goal.cpp
Expand Up @@ -334,7 +334,7 @@ void lua_goal::add_targets(std::back_insert_iterator< std::vector< target >> tar
{
*target_list = tg;
}
} catch(bad_enum_cast& e) {
} catch(const bad_enum_cast& e) {
ERR_AI_GOAL << "A Lua goal returned a target of an unknown type (\"" << e.value() << "\"; unfortunately, the engine cannot recover from this error. As a result, all targets returned by the goal have been lost.\n";
}

Expand Down
2 changes: 1 addition & 1 deletion src/ai/configuration.cpp
Expand Up @@ -172,7 +172,7 @@ bool configuration::get_side_config_from_file(const std::string& file, config& c
filesystem::scoped_istream stream = preprocess_file(filesystem::get_wml_location(file));
read(cfg, *stream);
LOG_AI_CONFIGURATION << "Reading AI configuration from file '" << file << "'" << std::endl;
} catch(config::error &) {
} catch(const config::error &) {
ERR_AI_CONFIGURATION << "Error while reading AI configuration from file '" << file << "'" << std::endl;
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ai/formula/ai.cpp
Expand Up @@ -86,7 +86,7 @@ ca_ptr formula_ai::load_candidate_action_from_config(const config& rc_action)
} else {
ERR_AI << "Unknown candidate action type: " << type << std::endl;
}
} catch(formula_error& e) {
} catch(const formula_error& e) {
handle_exception(e, "Error while registering candidate action '" + name + "'");
}
return new_ca;
Expand Down Expand Up @@ -144,7 +144,7 @@ formula_ptr formula_ai::create_optional_formula(const std::string& formula_strin
try{
return formula::create_optional_formula(formula_string, &function_table_);
}
catch(formula_error& e) {
catch(const formula_error& e) {
handle_exception(e);
return wfl::formula_ptr();
}
Expand Down Expand Up @@ -656,7 +656,7 @@ void formula_ai::on_create(){
create_optional_formula(func["precondition"]),
args);
}
catch(formula_error& e) {
catch(const formula_error& e) {
handle_exception(e, "Error while registering function '" + name + "'");
}
}
Expand Down Expand Up @@ -752,7 +752,7 @@ config formula_ai::to_config() const
{
try {
str = i->second.serialize_to_string();
} catch (type_error&) {
} catch(const type_error&) {
WRN_AI << "variable ["<< i->first <<"] is not serializable - it will not be persisted across savegames"<<std::endl;
continue;
}
Expand Down
10 changes: 5 additions & 5 deletions src/ai/formula/candidates.cpp
Expand Up @@ -46,10 +46,10 @@ int base_candidate_action::execute_formula(const const_formula_ptr& formula,
int res = 0;
try {
res = (formula::evaluate(formula, callable)).as_int();
} catch(formula_error& e) {
} catch(const formula_error& e) {
ai->handle_exception(e);
res = 0;
} catch(type_error& e) {
} catch(const type_error& e) {
res = 0;
ERR_AI << "formula type error while evaluating candidate action: " << e.message << std::endl;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ void move_candidate_action::evaluate(ai::formula_ai* ai, unit_map& units)
else
filtered_units=my_units;
}
catch(formula_error& e) {
catch(const formula_error& e) {
ai->handle_exception(e, "Error while executing filter formula for '" + get_name() + "' Candidate Action");
return;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ void attack_candidate_action::evaluate(ai::formula_ai* ai, unit_map& units)
else
filtered_enemy_units = enemy_units;
}
catch(formula_error& e) {
catch(const formula_error& e) {
ai->handle_exception(e, "Error while executing filter formula for '" + get_name() + "' Candidate Action");
return;
}
Expand All @@ -195,7 +195,7 @@ void attack_candidate_action::evaluate(ai::formula_ai* ai, unit_map& units)
if( !(filtered_enemy_units.num_elements() && filtered_my_units.num_elements() ) )
return;
}
catch(type_error& e) {
catch(const type_error& e) {
ERR_AI << "Error while executing filter formulas for '" + get_name() + "' Candidate Action: " << e.message << std::endl;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/formula/stage_unit_formulas.cpp
Expand Up @@ -78,7 +78,7 @@ bool stage_unit_formulas::do_play_stage()
fai_.handle_exception( e, "Unit priority formula error for unit: '" + i->type_id() + "' standing at (" + std::to_string(i->get_location().wml_x()) + "," + std::to_string(i->get_location().wml_y()) + ")");

priority = 0;
} catch(wfl::type_error& e) {
} catch(const wfl::type_error& e) {
priority = 0;
ERR_AI << "formula type error while evaluating unit priority formula " << e.message << std::endl;
}
Expand Down
10 changes: 5 additions & 5 deletions src/campaign_server/campaign_server.cpp
Expand Up @@ -589,14 +589,14 @@ void server::handle_request_campaign_list(const server::request& req)
try {
before = before + lexical_cast<time_t>(req.cfg["before"]);
before_flag = true;
} catch(bad_lexical_cast) {}
} catch(const bad_lexical_cast&) {}

bool after_flag = false;
time_t after = epoch;
try {
after = after + lexical_cast<time_t>(req.cfg["after"]);
after_flag = true;
} catch(bad_lexical_cast) {}
} catch(const bad_lexical_cast&) {}

const std::string& name = req.cfg["name"];
const std::string& lang = req.cfg["language"];
Expand Down Expand Up @@ -1009,13 +1009,13 @@ int main()
const std::string cfg_path = filesystem::normalize_path("server.cfg");

campaignd::server(cfg_path).run();
} catch(config::error& /*e*/) {
} catch(const config::error& /*e*/) {
std::cerr << "Could not parse config file\n";
return 1;
} catch(filesystem::io_exception& e) {
} catch(const filesystem::io_exception& e) {
std::cerr << "File I/O error: " << e.what() << "\n";
return 2;
} catch(std::bad_function_call& /*e*/) {
} catch(const std::bad_function_call& /*e*/) {
std::cerr << "Bad request handler function call\n";
return 4;
}
Expand Down
6 changes: 3 additions & 3 deletions src/commandline_options.cpp
Expand Up @@ -518,7 +518,7 @@ void commandline_options::parse_resolution_ ( const std::string& resolution_stri
try {
xres = std::stoi(tokens[0]);
yres = std::stoi(tokens[1]);
} catch(std::invalid_argument &) {
} catch(const std::invalid_argument &) {
throw bad_commandline_resolution(resolution_string);
}

Expand All @@ -541,7 +541,7 @@ std::vector<std::pair<unsigned int,std::string>> commandline_options::parse_to_u
unsigned int temp;
try {
temp = lexical_cast<unsigned int>(tokens[0]);
} catch (bad_lexical_cast &) {
} catch (const bad_lexical_cast &) {
throw bad_commandline_tuple(s, expected_format);
}

Expand All @@ -566,7 +566,7 @@ std::vector<std::tuple<unsigned int,std::string,std::string>> commandline_option
unsigned int temp;
try {
temp = lexical_cast<unsigned int>(tokens[0]);
} catch (bad_lexical_cast &) {
} catch (const bad_lexical_cast &) {
throw bad_commandline_tuple(s, expected_format);
}

Expand Down
12 changes: 6 additions & 6 deletions src/config_cache.cpp
Expand Up @@ -175,9 +175,9 @@ void config_cache::read_cache(const std::string& file_path, config& cfg)

dir_checksum = filesystem::file_tree_checksum(checksum_cfg);
}
} catch(config::error&) {
} catch(const config::error&) {
ERR_CACHE << "cache checksum is corrupt" << std::endl;
} catch(filesystem::io_exception&) {
} catch(const filesystem::io_exception&) {
ERR_CACHE << "error reading cache checksum" << std::endl;
}
}
Expand All @@ -199,11 +199,11 @@ void config_cache::read_cache(const std::string& file_path, config& cfg)
}

return;
} catch(config::error& e) {
} catch(const config::error& e) {
ERR_CACHE << "cache " << fname << extension << " is corrupt. Loading from files: "<< e.message << std::endl;
} catch(filesystem::io_exception&) {
} catch(const filesystem::io_exception&) {
ERR_CACHE << "error reading cache " << fname << extension << ". Loading from files" << std::endl;
} catch (boost::iostreams::gzip_error& e) {
} catch (const boost::iostreams::gzip_error& e) {
//read_file -> ... -> read_gz can throw this exception.
ERR_CACHE << "cache " << fname << extension << " is corrupt. Error code: " << e.error() << std::endl;
}
Expand All @@ -227,7 +227,7 @@ void config_cache::read_cache(const std::string& file_path, config& cfg)

filesystem::data_tree_checksum().write(checksum_cfg);
write_file(fname_checksum, checksum_cfg);
} catch(filesystem::io_exception&) {
} catch(const filesystem::io_exception&) {
ERR_CACHE << "could not write to cache '" << fname << "'" << std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/display.cpp
Expand Up @@ -341,7 +341,7 @@ void display::init_flags_for_side_internal(size_t n, const std::string& side_col
str = sub_items.front();
try {
time = std::max<int>(1, std::stoi(sub_items.back()));
} catch(std::invalid_argument&) {
} catch(const std::invalid_argument&) {
ERR_DP << "Invalid time value found when constructing flag for side " << n << ": " << sub_items.back() << "\n";
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -174,10 +174,10 @@ EXIT_STATUS editor_controller::main_loop()
while (!do_quit_) {
play_slice();
}
} catch (editor_exception& e) {
} catch (const editor_exception& e) {
gui2::show_transient_message(_("Fatal error"), e.what());
return EXIT_ERROR;
} catch (wml_exception& e) {
} catch (const wml_exception& e) {
e.show();
}
return quit_mode_;
Expand All @@ -193,7 +193,7 @@ void editor_controller::do_screenshot(const std::string& screenshot_filename /*
if(screenshot.null() || image::save_image(screenshot, screenshot_filename) != image::save_result::success) {
ERR_ED << "Screenshot creation failed!\n";
}
} catch (wml_exception& e) {
} catch (const wml_exception& e) {
e.show();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editor_main.cpp
Expand Up @@ -62,7 +62,7 @@ EXIT_STATUS start(const config& game_conf, const std::string& filename /* = "" *
if (!take_screenshot)
e = editor.main_loop();

} catch (editor_exception& e) {
} catch(const editor_exception& e) {
ERR_ED << "Editor exception in editor::start: " << e.what() << std::endl;
throw;
}
Expand Down

0 comments on commit 0bf09c8

Please sign in to comment.