6.2 Errors and exceptions¶
Exceptions and response codes
The functions of API for MATLAB will throw a MATLAB exception in case of errors. There are two types of exception messages:
Simple calls to functions setting up the model will throw an exception with a single string describing the error, for example missing or incorrect data.
Calls to functions such as
mosekmodel.solve
which invoke the MOSEK library will throw exceptions with a standardized string containing a MOSEK response code and an explanation (see below). The response code can be cross-referenced against the list of MOSEK response codes in Sec. 13.6 (Response codes).
For this reason it is a good idea to call MOSEK functions in a try-catch block. The one case where it is extremely important to check for an exception is when calling mosekmodel.solve
. For more information see Sec. 6.1 (Accessing the solution).
As an example, consider:
model = mosekmodel(objective = [NaN], numvar = 1);
try
model.solve();
catch ME
fprintf("An error during solve(). Message:\n%s\n", ME.message);
end
This will produce as output:
An error during solve(). Message:
MSK_RES_ERR_NAN_IN_C(1470):The objective vector c contains an invalid value for variable '' (0).
In many cases (especially related to licensing) a much more verbose error message will be printed to the log.
Parsing the exception message.
An exception message produced by MOSEK during a call to mosekmodel.solve
has the format
ERROR_NAME(ERROR_CODE):MESSAGE
where the ERROR_NAME
is a string and ERROR_CODE
is an integer, both of which can be cross-referenced against Sec. 13.6 (Response codes). The integer value is not guaranteed to remain constant between MOSEK versions, so it is recommended to test for equality of strings in ERROR_NAME
.
The message can be parsed into individual components as follows:
model = mosekmodel(objective = [NaN], numvar = 1);
try
model.solve();
catch ME
expr = "([A-Z_]*)\(([0-9]*)\):(.*)";
[tokens, matches] = regexp(ME.message, expr, "tokens", "match");
fprintf("Error name: %s\n", tokens{1}{1});
fprintf("Code: %.0f\n", str2double(tokens{1}{2}));
fprintf("Message: %s\n", tokens{1}{3});
end
which leads to an output such as
Error name: MSK_RES_ERR_NAN_IN_C
Code: 1470
Message: The objective vector c contains an invalid value for variable '' (0).
Optimizer errors and warnings
The optimizer may also produce warning messages. They indicate non-critical but important events, that will not prevent solver execution, but may be an indication that something in the optimization problem might be improved. Warning messages are normally printed to a log stream (see Sec. 6.3 (Input/Output)). A typical warning is, for example:
MOSEK warning 53: A numerically large upper bound value 6.6e+09 is specified for constraint 'C69200' (46020).
Error and solution status handling example
Below is a source code example with a simple framework for handling major errors when assessing and retrieving the solution to a conic optimization problem.
% Solve the model
try
model.solve();
catch ME
warning("An error during optimization; handle it here.");
rethrow(ME);
end
% We check if the interior-point solution exists and what status it has
[exists, prosta, solsta] = model.hassolution("interior");
if exists
disp("Solved the problem with statuses:");
disp(prosta);
disp(solsta);
switch solsta
case "OPTIMAL"
disp("Optimal solution found:");
x = model.getsolution("interior");
disp(x);
case "PRIM_INFEAS_CER"
disp("The problem is primal infeasible.");
case "DUAL_INFEAS_CER"
disp("The problem is dual infeasible.");
case "UNKNOWN"
disp("Solution status UNKNOWN. This could indicate numerical issues");
default
disp("Another solution status:")
disk(solsta)
end
else
warning("Solution does not exists");
end