function [Objective, ConstraintViolations, UserData, ErrorConstraintViolation, ObjectiveEvaluationTime] = callObjNormally(this, X)
% Call the objective fcn with the correct nargout, catching
% errors and timing runtime
%

%   Copyright 2016-2022 The MathWorks, Inc.

StartTic = tic;
ErrorConstraintViolation = -1;
try
    switch this.PrivOptions.ObjectiveNargout
        case 1
            Objective = this.ObjectiveFcn(conditionalizeX(this, X));
            ConstraintViolations = [];
            UserData = [];
        case 2
            [Objective, ConstraintViolations] = this.ObjectiveFcn(conditionalizeX(this, X));
            UserData = [];
        otherwise
            [Objective, ConstraintViolations, UserData] = this.ObjectiveFcn(conditionalizeX(this, X));
    end
catch msg
    if isequal(msg.identifier, 'MATLAB:unassignedOutputs') && this.PrivOptions.NumCoupledConstraints > 0
        bayesoptim.err('ObjectiveNargoutWrong', this.PrivOptions.NumCoupledConstraints);
    else
        rethrow(msg);
    end
end
[Objective, ConstraintViolations, ErrorConstraintViolation, ObjectiveEvaluationTime] ...
    = finishObjEval(this, Objective, ConstraintViolations, ErrorConstraintViolation, StartTic);
end
