function throwIfComplexInt(errorIdOrMException, varargin)
%THROWIFCOMPLEXINT Throw if any input is a complex integer
% Common helper to throw a specific error message if any of the
% inputs varargin is a complex integer. errorIdOrMException can either be an
% error ID or an MException.

%   Copyright 2017-2022 The MathWorks, Inc.

if any(cellfun(@(x)(isinteger(x) && ~isreal(x)), varargin))
    % Either all or non of the workers will enter this branch. However, the
    % communications to get the results of isreal and isinteger might have
    % been finished slightly delayed on some of the workers. Hence, we need
    % to synchronize before actually throwing the error, similar to the
    % approach in syncOnError, to prevent stalling.
    spmdBarrier();
    
    % Create MException from error ID if necessary
    if ~isa(errorIdOrMException, 'MException')
        errorIdOrMException = MException(message(errorIdOrMException));
    end
    throwAsCaller(dispatchUndefinedFunctionException(errorIdOrMException));
end
end