Main Content

rethrow

이전 caught exception 다시 발생

설명

예제

rethrow(exception)은 이전 caught exception인 exception을 다시 발생시킵니다. MATLAB®은 일반적으로 현재 실행 중인 프로그램을 종료하여 오류에 응답합니다. 그러나 try/catch 블록을 사용하여 예외를 포착할 수 있습니다. 이 경우 프로그램 종료가 중단되므로, 사용자가 고유의 오류 처리 절차를 실행할 수 있습니다. 프로그램을 종료하고 예외를 다시 표시하려면 rethrow 문을 사용하여 catch 블록을 끝내십시오.

rethrow는 스택 추적을 error, assert, throw와 다르게 처리합니다. rethrow는 MATLAB이 함수를 실행하는 스택을 생성하는 대신 원래 예외 정보를 유지하므로, 원래 오류의 원인을 다시 추적할 수 있습니다.

예제

모두 축소

입력값 없이 surf를 호출하여 MATLAB에서 오류가 발생하도록 합니다. 예외를 포착하고, 오류 ID를 표시한 다음, 예외를 다시 발생시킵니다.

try
    surf
catch ME
    disp(['ID: ' ME.identifier])
    rethrow(ME)
end
ID: MATLAB:narginchk:notEnoughInputs
Error using surf (line 49)
Not enough input arguments.

작업 폴더에 함수 combineArrays를 만듭니다.

function C = combineArrays(A,B)
try
    C = catAlongDim1(A,B);       % Line 3
catch exception
    throw(exception)             % Line 5
end
end

function V = catAlongDim1(V1,V2)
V = cat(1,V1,V2);                % Line 10
end

다른 크기의 배열을 갖는 combineArrays 함수를 호출합니다.

A = 1:5;
B = 1:4;

combineArrays(A,B)
Error using combineArrays
Dimensions of matrices being concatenated are not consistent.

스택은 MATLAB이 예외를 발생시킨 라인이 5임을 나타냅니다.

combineArrays 함수의 라인 5에서 throw(exception)rethrow(exception)으로 바꾸고 함수를 다시 호출합니다.

combineArrays(A,B)
Error using cat
Dimensions of matrices being concatenated are not consistent.

Error in combineArrays>catAlongDim1
V = cat(1,V1,V2);       

Error in combineArrays
    C = catAlongDim1(A,B);      

rethrow 함수는 원래 스택을 유지하고 오류가 라인 3에 있음을 나타냅니다.

입력 인수

모두 축소

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

버전 내역

R2007b에 개발됨