Skip to content

Commit bd3b8f6

Browse files
authored
Handle properly the exceptions returned by FGCondition (#1244)
1 parent 91ebed4 commit bd3b8f6

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/input_output/FGScript.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,12 @@ bool FGScript::LoadScript(const SGPath& script, double default_dT,
241241

242242
// Process the conditions
243243
Element* condition_element = event_element->FindElement("condition");
244-
if (condition_element != 0) {
244+
if (condition_element) {
245245
try {
246246
newCondition = new FGCondition(condition_element, PropertyManager);
247-
} catch(string& str) {
248-
cout << endl << fgred << str << reset << endl << endl;
247+
} catch(BaseException& e) {
248+
cerr << condition_element->ReadFrom()
249+
<< fgred << e.what() << reset << endl << endl;
249250
delete newEvent;
250251
return false;
251252
}

src/math/FGCondition.cpp

+8-17
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ FGCondition::FGCondition(Element* element, std::shared_ptr<FGPropertyManager> Pr
6565
if (logic == "OR") Logic = eOR;
6666
else if (logic == "AND") Logic = eAND;
6767
else { // error
68-
cerr << element->ReadFrom()
69-
<< "Unrecognized LOGIC token " << logic << endl;
70-
throw BaseException("FGCondition: unrecognized logic value:'" + logic + "'");
68+
throw BaseException("FGCondition: unrecognized LOGIC token:'" + logic + "'");
7169
}
7270
} else {
7371
Logic = eAND; // default
@@ -87,21 +85,14 @@ FGCondition::FGCondition(Element* element, std::shared_ptr<FGPropertyManager> Pr
8785
string tagName = condition_element->GetName();
8886

8987
if (tagName != elName) {
90-
cerr << condition_element->ReadFrom()
91-
<< "Unrecognized tag <" << tagName << "> in the condition statement."
92-
<< endl;
93-
throw BaseException("FGCondition: unrecognized tag:'" + tagName + "'");
88+
throw BaseException("FGCondition: unrecognized TAG:'" + tagName + "' in the condition statement.");
9489
}
9590

9691
conditions.push_back(make_shared<FGCondition>(condition_element, PropertyManager));
9792
condition_element = element->GetNextElement();
9893
}
9994

100-
if (conditions.empty()) {
101-
cerr << element->ReadFrom()
102-
<< "Empty conditional" << endl;
103-
throw BaseException("Empty conditional");
104-
}
95+
if (conditions.empty()) throw BaseException("Empty conditional");
10596

10697
Debug(0);
10798
}
@@ -143,11 +134,11 @@ FGCondition::FGCondition(const string& test, std::shared_ptr<FGPropertyManager>
143134
conditional = test_strings[1];
144135
TestParam2 = new FGParameterValue(test_strings[2], PropertyManager, el);
145136
} else {
146-
cerr << el->ReadFrom()
147-
<< " Conditional test is invalid: \"" << test
148-
<< "\" has " << test_strings.size() << " elements in the "
149-
<< "test condition." << endl;
150-
throw BaseException("FGCondition: incorrect number of test elements:" + std::to_string(test_strings.size()));
137+
ostringstream s;
138+
s << " Conditional test is invalid: \"" << test
139+
<< "\" has " << test_strings.size() << " elements in the "
140+
<< "test condition.\n";
141+
throw BaseException(s.str());
151142
}
152143

153144
assert(Comparison == ecUndef);

src/models/flight_control/FGDistributor.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,21 @@ FGDistributor::FGDistributor(FGFCS* fcs, Element* element)
6363
if (type_string == "inclusive") Type = eInclusive;
6464
else if (type_string == "exclusive") Type = eExclusive;
6565
else {
66-
throw("Not a known Distributor type, "+type_string);
66+
throw BaseException("Not a known Distributor type, "+type_string);
6767
}
6868

6969
Element* case_element = element->FindElement("case");
7070
while (case_element) {
7171
Case* current_case = new Case;
7272
Element* test_element = case_element->FindElement("test");
73-
if (test_element) current_case->SetTest(new FGCondition(test_element, PropertyManager));
73+
try {
74+
if (test_element) current_case->SetTest(new FGCondition(test_element, PropertyManager));
75+
} catch (BaseException& e) {
76+
FGXMLLogging log(fcs->GetExec()->GetLogger(), test_element, LogLevel::FATAL);
77+
log << LogFormat::RED << e.what() << LogFormat::RESET << "\n\n";
78+
delete current_case;
79+
throw;
80+
}
7481
Element* prop_val_element = case_element->FindElement("property");
7582
while (prop_val_element) {
7683
string value_string = prop_val_element->GetAttributeValue("value");

0 commit comments

Comments
 (0)