7.2 Errors and exceptions¶
Response codes
The function mosek returns a response code (and its human-readable description), informing if optimization was performed correctly, and if not, what error occurred. The expected response, indicating successful execution, is always "MSK_RES_OK". Typical errors include:
- referencing a nonexisting variable (for example with too large index), 
- incompatible dimensions of input data matrices, 
- NaN in the input data, 
- duplicate conic variable, 
- error in the optimizer. 
A full list of response codes, error, warning and termination codes can be found in the API reference. For example, if the objective vector contains a NaN then
r <- mosek(prob)
r$response
will produce as output:
$code
[1] 1470
$msg
[1] "MSK_RES_ERR_NAN_IN_C: c contains an invalid floating point value, i.e. a NaN."
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. 7.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).
Warnings can also be suppressed by setting the MSK_IPAR_MAX_NUM_WARNINGS parameter to zero, if they are well-understood.
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.
response <- function()
{
    # In this example we set up a simple problem 
    prob <- setupProblem()
    # (Optionally) Uncomment the next line to get solution status Unknown
    # prob$iparam <- list(INTPNT_MAX_ITERATIONS=1)
    # Perform the optimization.
    r <- mosek(prob, list(verbose=0))
    # Use the line below instead to get more log output
    #r <- mosek(prob, list(verbose=10))
    # Expected result: The solution status of the interior-point solution is optimal.
    # Check if there was a fatal error
    if (r$response$code != 0 && startsWith(r$response$msg, "MSK_RES_ERR"))
    {
        print(sprintf("Optimization error: %s (%d),", r$response$msg, r$response$code))
    }
    else
    {
        if (r$sol$itr$solsta == 'OPTIMAL')
        {
            print('An optimal interior-point solution is located:')
            print(r$sol$itr$xx)
        }
        else if (r$sol$itr$solsta == 'DUAL_INFEASIBLE_CER')
        {
            print('Dual infeasibility certificate found.')
        }
        else if (r$sol$itr$solsta == 'PRIMAL_INFEASIBLE_CER')
        {
            print('Primal infeasibility certificate found.')
        }
        else if (r$sol$itr$solsta == 'UNKNOWN')
        { 
            # The solutions status is unknown. The termination code 
            # indicates why the optimizer terminated prematurely. 
            print('The solution status is unknown.')
            print(sprintf('Termination code: %s (%d).', r$response$msg, r$response$code))
        }
        else
        {
            printf('An unexpected solution status is obtained.')
        }
    }
}
