@@ -83,10 +83,19 @@ void GBDT::Init(const Config* config, const Dataset* train_data, const Objective
83
83
// load forced_splits file
84
84
if (!config->forcedsplits_filename .empty ()) {
85
85
std::ifstream forced_splits_file (config->forcedsplits_filename .c_str ());
86
- std::stringstream buffer;
87
- buffer << forced_splits_file.rdbuf ();
88
- std::string err;
89
- forced_splits_json_ = Json::parse (buffer.str (), &err);
86
+ if (!forced_splits_file.good ()) {
87
+ Log::Warning (" Forced splits file '%s' does not exist. Forced splits will be ignored." ,
88
+ config->forcedsplits_filename .c_str ());
89
+ } else {
90
+ std::stringstream buffer;
91
+ buffer << forced_splits_file.rdbuf ();
92
+ std::string err;
93
+ forced_splits_json_ = Json::parse (buffer.str (), &err);
94
+ if (!err.empty ()) {
95
+ Log::Fatal (" Failed to parse forced splits file '%s': %s" ,
96
+ config->forcedsplits_filename .c_str (), err.c_str ());
97
+ }
98
+ }
90
99
}
91
100
92
101
objective_function_ = objective_function;
@@ -823,13 +832,23 @@ void GBDT::ResetConfig(const Config* config) {
823
832
if (config_.get () != nullptr && config_->forcedsplits_filename != new_config->forcedsplits_filename ) {
824
833
// load forced_splits file
825
834
if (!new_config->forcedsplits_filename .empty ()) {
826
- std::ifstream forced_splits_file (
827
- new_config->forcedsplits_filename .c_str ());
828
- std::stringstream buffer;
829
- buffer << forced_splits_file.rdbuf ();
830
- std::string err;
831
- forced_splits_json_ = Json::parse (buffer.str (), &err);
832
- tree_learner_->SetForcedSplit (&forced_splits_json_);
835
+ std::ifstream forced_splits_file (new_config->forcedsplits_filename .c_str ());
836
+ if (!forced_splits_file.good ()) {
837
+ Log::Warning (" Forced splits file '%s' does not exist. Forced splits will be ignored." ,
838
+ new_config->forcedsplits_filename .c_str ());
839
+ forced_splits_json_ = Json ();
840
+ tree_learner_->SetForcedSplit (nullptr );
841
+ } else {
842
+ std::stringstream buffer;
843
+ buffer << forced_splits_file.rdbuf ();
844
+ std::string err;
845
+ forced_splits_json_ = Json::parse (buffer.str (), &err);
846
+ if (!err.empty ()) {
847
+ Log::Fatal (" Failed to parse forced splits file '%s': %s" ,
848
+ new_config->forcedsplits_filename .c_str (), err.c_str ());
849
+ }
850
+ tree_learner_->SetForcedSplit (&forced_splits_json_);
851
+ }
833
852
} else {
834
853
forced_splits_json_ = Json ();
835
854
tree_learner_->SetForcedSplit (nullptr );
0 commit comments