Skip to content

Commit d32ebc4

Browse files
committed
Updated exception handling
1 parent a0039a5 commit d32ebc4

2 files changed

Lines changed: 32 additions & 21 deletions

File tree

include/cosim.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ cosim_execution* cosim_execution_create(
160160
* \param [in] relTolerance Relative tolerance for deciding mismatch in the residual power
161161
* \param [in] pGain Proportional value in the PI controller
162162
* \param [in] iGain Integral value in the PI controller
163-
* \returns A pointer to a new instance of cosim_algorithm
163+
* \returns A pointer to a new instance of cosim_algorithm, or NULL if an error occurred.
164164
*/
165165
cosim_algorithm* cosim_ecco_algorithm_create(
166166
double safetyFactor,
@@ -199,6 +199,7 @@ int cosim_ecco_add_power_bond(
199199
* Creates a fixed step algorithm
200200
* \param [in] stepSize
201201
* The execution step size.
202+
* \returns A pointer to a new instance of cosim_algorithm, or NULL if an error occurred.
202203
*/
203204
cosim_algorithm* cosim_fixed_step_algorithm_create(cosim_duration stepSize);
204205

src/cosim.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,40 @@ cosim_algorithm* cosim_ecco_algorithm_create(
171171
double pGain,
172172
double iGain)
173173
{
174-
cosim::ecco_algorithm_params ecco_params = {
175-
safetyFactor,
176-
cosim::to_duration(stepSize),
177-
cosim::to_duration(minStepSize),
178-
cosim::to_duration(maxStepSize),
179-
minChangeRate,
180-
maxChangeRate,
181-
absTolerance,
182-
relTolerance,
183-
pGain,
184-
iGain};
185-
186-
auto algo = std::make_unique<cosim_algorithm>();
187-
algo->algorithm = std::make_shared<cosim::ecco_algorithm>(ecco_params);
188-
algo->type = ECCO;
189-
return algo.release();
174+
try{
175+
cosim::ecco_algorithm_params ecco_params = {
176+
safetyFactor,
177+
cosim::to_duration(stepSize),
178+
cosim::to_duration(minStepSize),
179+
cosim::to_duration(maxStepSize),
180+
minChangeRate,
181+
maxChangeRate,
182+
absTolerance,
183+
relTolerance,
184+
pGain,
185+
iGain};
186+
187+
auto algo = std::make_unique<cosim_algorithm>();
188+
algo->algorithm = std::make_shared<cosim::ecco_algorithm>(ecco_params);
189+
algo->type = ECCO;
190+
return algo.release();
191+
} catch (...) {
192+
handle_current_exception();
193+
return nullptr;
194+
}
190195
}
191196

192197
cosim_algorithm* cosim_fixed_step_algorithm_create(cosim_duration stepSize)
193198
{
194-
auto algo = std::make_unique<cosim_algorithm>();
195-
algo->algorithm = std::make_shared<cosim::fixed_step_algorithm>(to_duration(stepSize));
196-
algo->type = FIXED_STEP;
197-
return algo.release();
199+
try{
200+
auto algo = std::make_unique<cosim_algorithm>();
201+
algo->algorithm = std::make_shared<cosim::fixed_step_algorithm>(to_duration(stepSize));
202+
algo->type = FIXED_STEP;
203+
return algo.release();
204+
} catch (...) {
205+
handle_current_exception();
206+
return nullptr;
207+
}
198208
}
199209

200210
struct cosim_execution_s

0 commit comments

Comments
 (0)