Skip to content

Commit d21c4dc

Browse files
committed
MAINT: Use exceptions to propagate errors
1 parent 49a27c5 commit d21c4dc

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/presolve/HPresolve.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <limits>
1919

2020
#include "Highs.h"
21+
#include "HighsExceptions.h"
2122
#include "io/HighsIO.h"
2223
#include "lp_data/HConst.h"
2324
#include "lp_data/HStruct.h"
@@ -3914,7 +3915,14 @@ HPresolve::Result HPresolve::presolve(HighsPostsolveStack& postsolve_stack) {
39143915
}
39153916
};
39163917

3917-
HPRESOLVE_CHECKED_CALL(initialRowAndColPresolve(postsolve_stack));
3918+
try {
3919+
HPRESOLVE_CHECKED_CALL(initialRowAndColPresolve(postsolve_stack));
3920+
} catch (const DataStackOverflow& e) {
3921+
highsLogUser(options->log_options, HighsLogType::kInfo,
3922+
"Problem is too large to be presolved\n");
3923+
// Here we re-throw the error
3924+
throw;
3925+
}
39183926

39193927
HighsInt numParallelRowColCalls = 0;
39203928
#if ENABLE_SPARSIFY_FOR_LP

src/presolve/HPresolve.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "util/HighsHash.h"
3333
#include "util/HighsLinearSumBounds.h"
3434
#include "util/HighsMatrixSlice.h"
35+
#include "util/HighsExceptions.h"
3536

3637
namespace presolve {
3738

src/presolve/HighsPostsolveStack.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <tuple>
2525
#include <vector>
2626

27+
#include "HighsExceptions.h"
2728
#include "lp_data/HConst.h"
2829
#include "lp_data/HStruct.h"
2930
#include "lp_data/HighsOptions.h"
@@ -367,17 +368,23 @@ class HighsPostsolveStack {
367368
template <typename ColStorageFormat>
368369
void fixedColAtLower(HighsInt col, double fixValue, double colCost,
369370
const HighsMatrixSlice<ColStorageFormat>& colVec) {
370-
assert(std::isfinite(fixValue));
371-
colValues.clear();
372-
for (const HighsSliceNonzero& colVal : colVec)
373-
colValues.emplace_back(origRowIndex[colVal.index()], colVal.value());
374-
375-
reductionValues.push(FixedCol{fixValue, colCost, origColIndex[col],
376-
HighsBasisStatus::kLower});
377-
reductionValues.push(colValues);
378-
reductionAdded(ReductionType::kFixedCol);
371+
try {
372+
assert(std::isfinite(fixValue));
373+
colValues.clear();
374+
for (const HighsSliceNonzero& colVal : colVec)
375+
colValues.emplace_back(origRowIndex[colVal.index()], colVal.value());
376+
377+
reductionValues.push(FixedCol{fixValue, colCost, origColIndex[col],
378+
HighsBasisStatus::kLower});
379+
reductionValues.push(colValues);
380+
reductionAdded(ReductionType::kFixedCol);
381+
} catch (const DataStackOverflow& e) {
382+
std::cerr << "Memory allocation failed while processing fixedColAtLower: "
383+
<< e.what() << std::endl;
384+
// Rethrow.
385+
throw;
386+
}
379387
}
380-
381388
template <typename ColStorageFormat>
382389
void fixedColAtUpper(HighsInt col, double fixValue, double colCost,
383390
const HighsMatrixSlice<ColStorageFormat>& colVec) {

src/util/HighsDataStack.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
#define UTIL_HIGHS_DATA_STACK_H_
1919

2020
#include <cstring>
21+
#include <string>
2122
#include <type_traits>
2223
#include <vector>
2324

25+
#include "util/HighsExceptions.h"
2426
#include "util/HighsInt.h"
2527

2628
#if __GNUG__ && __GNUC__ < 5
@@ -40,10 +42,18 @@ class HighsDataStack {
4042
typename std::enable_if<IS_TRIVIALLY_COPYABLE(T), int>::type = 0>
4143
void push(const T& r) {
4244
HighsInt dataSize = data.size();
43-
data.resize(dataSize + sizeof(T));
45+
HighsInt newSize = dataSize + sizeof(T);
46+
try {
47+
data.resize(newSize);
48+
} catch (const std::length_error& e) {
49+
throw DataStackOverflow(
50+
"Failed to resize the vector. Requested new size: " +
51+
std::to_string(newSize) + ". Size to add is " +
52+
std::to_string(sizeof(T)) + "for "+
53+
". Current size: " + std::to_string(data.size()) + ".");
54+
}
4455
std::memcpy(data.data() + dataSize, &r, sizeof(T));
4556
}
46-
4757
template <typename T,
4858
typename std::enable_if<IS_TRIVIALLY_COPYABLE(T), int>::type = 0>
4959
void pop(T& r) {

0 commit comments

Comments
 (0)