function exitOnException( exitStatus )
%

%   Copyright 2012-2020 The MathWorks, Inc.

try
    % If no exit status is defined then assume 1
    if nargin < 1
        exitStatus = 1;
    end

    dctSchedulerMessage(0, 'About to exit with code: %d', exitStatus);

    % This method will flush the handlers to ensure that any remaining
    % published messages are written to file before exiting.
    parallel.internal.logging.flushAllHandlers()

    [isWorker, isDebugWorker] = system_dependent('isdmlworker');
    if isWorker && ~isDebugWorker
        exit(exitStatus, 'force');
    end
catch
    % If anything goes wrong, err on the side of caution and make sure this
    % MATLAB exits
    exit(1, 'force');
end
end