Main Content

addCause

예외의 추가적인 원인 기록

설명

예제

baseException = addCause(baseException,causeException)은 기존 MException 객체인 baseExceptioncause 속성에 causeException을 추가(Append)하여 baseException을 수정합니다. try/catch 문에서 결과 예외를 포착하면, 예외가 추가(Append)된 모든 원인 기록과 함께, 오류 진단에 사용할 수 있는 기본 예외가 만들어집니다.

예제

모두 축소

배열을 만들고, 논리형 배열을 사용하여 배열의 요소를 참조합니다.

A = [13 42; 7 20];
idx = [1 0 1; 0 1 0];

오류에 대한 일반 정보를 제공하는 예외를 만듭니다. 인덱스 배열을 테스트하고 실패 원인에 대한 자세한 정보와 함께 예외를 추가합니다.

try
    A(idx);
catch
    errID = 'MYFUN:BadIndex';
    msg = 'Unable to index into array.';
    baseException = MException(errID,msg);
    
    try
        assert(islogical(idx),'MYFUN:notLogical',...
            'Indexing array is not logical.')
    catch causeException
        baseException = addCause(baseException,causeException);
    end
    
    if any(size(idx) > size(A))
        errID = 'MYFUN:incorrectSize';
        msg = 'Indexing array is too large.';
        causeException2 = MException(errID,msg);
        baseException = addCause(baseException,causeException2);
    end
    throw(baseException)
end
Unable to index into array.

Caused by:
    Indexing array is not logical.
    Indexing array is too large.

baseException 객체를 검토합니다.

baseException
baseException = 

  MException with properties:

    identifier: 'MYFUN:BadIndex'
       message: 'Unable to index into array.'
         cause: {2x1 cell}
         stack: [0x1 struct]

cause 속성의 값은 2x1 셀형 배열입니다.

예외의 첫 번째 원인을 검토합니다.

baseException.cause{1}
ans = 

  MException with properties:

    identifier: 'MYFUN:notLogical'
       message: 'Indexing array is not logical.'
         cause: {0x1 cell}
         stack: [0x1 struct]

예외의 두 번째 원인을 검토합니다.

baseException.cause{2}
ans = 

  MException with properties:

    identifier: 'MYFUN:incorrectSize'
       message: 'Indexing array is too large.'
         cause: {}
         stack: [0x1 struct]

입력 인수

모두 축소

오류의 주 원인과 위치를 포함하는 주 예외로, MException 객체로 지정됩니다.

baseException과 관련된 오류의 원인과 위치를 포함하는 관련 예외로, MException 객체로 지정됩니다.

확장 기능

스레드 기반 환경
MATLAB®의 backgroundPool을 사용해 백그라운드에서 코드를 실행하거나 Parallel Computing Toolbox™의 ThreadPool을 사용해 코드 실행 속도를 높일 수 있습니다.

버전 내역

R2007b에 개발됨