# Errors

When an MOI call fails on a model, precise errors should be thrown when possible instead of simply calling error with a message. The docstrings for the respective methods describe the errors that the implementation should throw in certain situations. This error-reporting system allows code to distinguish between internal errors (that should be shown to the user) and unsupported operations which may have automatic workarounds.

When an invalid index is used in an MOI call, an InvalidIndex is thrown:

MathOptInterface.InvalidIndexType
struct InvalidIndex{IndexType<:Index} <: Exception
index::IndexType
end

An error indicating that the index index is invalid.

When an invalid result index is used to retrieve an attribute, a ResultIndexBoundsError is thrown:

MathOptInterface.ResultIndexBoundsErrorType
struct ResultIndexBoundsError{AttrType} <: Exception
attr::AttrType
result_count::Int
end

An error indicating that the requested attribute attr could not be retrieved, because the solver returned too few results compared to what was requested. For instance, the user tries to retrieve VariablePrimal(2) when only one solution is available, or when the model is infeasible and has no solution.

See also: check_result_index_bounds.

As discussed in JuMP mapping, for scalar constraint with a nonzero function constant, a ScalarFunctionConstantNotZero exception may be thrown:

MathOptInterface.ScalarFunctionConstantNotZeroType
struct ScalarFunctionConstantNotZero{T, F, S} <: Exception
constant::T
end

An error indicating that the constant part of the function in the constraint F-in-S is nonzero.

Some VariableIndex constraints cannot be combined on the same variable:

As discussed in AbstractCallback, trying to get attributes inside a callback may throw:

Trying to submit the wrong type of AbstractSubmittable inside an AbstractCallback (e.g., a UserCut inside a LazyConstraintCallback) will throw:

The rest of the errors defined in MOI fall in two categories represented by the following two abstract types:

The different UnsupportedError and NotAllowedError are the following errors:

MathOptInterface.SetAttributeNotAllowedType
struct SetAttributeNotAllowed{AttrType} <: NotAllowedError
attr::AttrType
message::String # Human-friendly explanation why the attribute cannot be set
end

An error indicating that the attribute attr is supported (see supports) but cannot be set for some reason (see the error string).

MathOptInterface.AddVariableNotAllowedType
struct AddVariableNotAllowed <: NotAllowedError
message::String # Human-friendly explanation why the attribute cannot be set
end

An error indicating that variables cannot be added to the model.

MathOptInterface.UnsupportedConstraintType
struct UnsupportedConstraint{F<:AbstractFunction, S<:AbstractSet} <: UnsupportedError
message::String # Human-friendly explanation why the attribute cannot be set
end

An error indicating that constraints of type F-in-S are not supported by the model, i.e. that supports_constraint returns false.

MathOptInterface.ModifyConstraintNotAllowedType
struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet,
C<:AbstractFunctionModification} <: NotAllowedError
constraint_index::ConstraintIndex{F, S}
change::C
message::String
end

An error indicating that the constraint modification change cannot be applied to the constraint of index ci.

MathOptInterface.ModifyObjectiveNotAllowedType
struct ModifyObjectiveNotAllowed{C<:AbstractFunctionModification} <: NotAllowedError
change::C
message::String
end

An error indicating that the objective modification change cannot be applied to the objective.

MathOptInterface.DeleteNotAllowedType
struct DeleteNotAllowed{IndexType <: Index} <: NotAllowedError
index::IndexType
message::String
end

An error indicating that the index index cannot be deleted.

MathOptInterface.SubmitNotAllowedType
struct SubmitNotAllowed{SubmitTyp<:AbstractSubmittable} <: NotAllowedError
sub::SubmitType
message::String # Human-friendly explanation why the attribute cannot be set
end

An error indicating that the submittable sub is supported (see supports) but cannot be added for some reason (see the error string).

Note that setting the ConstraintFunction of a VariableIndex constraint is not allowed: