Skip to content

Commit 2c032a7

Browse files
committed
Don't use try-catch
1 parent 64d2db6 commit 2c032a7

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/Utilities/matrix_of_constraints.jl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,11 @@ function modify_constants end
252252
row::Integer,
253253
col::Integer,
254254
new_coefficient,
255-
)
255+
)::Bool
256256
257257
Modify `coefficients` in-place to store `new_coefficient` at position
258-
`(row, col)`.
258+
`(row, col)`. Return `true` if the entry existed and was modified, and `false`
259+
if no entry exists at `(row, col)` in the sparse structure and `new_coefficient` is nonzero.
259260
260261
This function must be implemented to enable
261262
[`MOI.ScalarCoefficientChange`](@ref) for [`MatrixOfConstraints`](@ref).
@@ -719,15 +720,20 @@ function MOI.modify(
719720
ci::MOI.ConstraintIndex,
720721
change::MOI.ScalarCoefficientChange,
721722
)
722-
try
723-
modify_coefficients(
724-
model.coefficients,
725-
rows(model, ci),
726-
change.variable.value,
727-
change.new_coefficient,
723+
if !modify_coefficients(
724+
model.coefficients,
725+
rows(model, ci),
726+
change.variable.value,
727+
change.new_coefficient,
728+
)
729+
throw(
730+
MOI.ModifyConstraintNotAllowed(
731+
ci,
732+
change,
733+
"cannot set a new non-zero coefficient because no entry " *
734+
"exists in the sparse matrix of `MatrixOfConstraints`",
735+
),
728736
)
729-
catch
730-
throw(MOI.ModifyConstraintNotAllowed(ci, change))
731737
end
732738
return
733739
end

src/Utilities/sparse_matrix.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,9 @@ function modify_coefficients(
172172
r = _shift(row, OneBasedIndexing(), _indexing(A))
173173
if idx <= last(range) && A.rowval[idx] == r
174174
A.nzval[idx] = new_coefficient
175-
elseif !iszero(new_coefficient)
176-
error(
177-
"Cannot set a new non-zero coefficient at ($row, $col) because " *
178-
"no entry exists in the sparse matrix. Adding new entries to a " *
179-
"`MutableSparseMatrixCSC` after `final_touch` is not supported.",
180-
)
175+
return true
181176
end
182-
return
177+
return iszero(new_coefficient)
183178
end
184179

185180
"""

0 commit comments

Comments
 (0)