In try~catch, can I use multiple catch like JAVA?
조회 수: 46 (최근 30일)
이전 댓글 표시
try ... catch ... catch ...
This form is impossible.
Now, I used java and matlab mix, so need the try~catch like that. Could I use that form??
Thanks
댓글 수: 0
답변 (2개)
Jan
2015년 11월 2일
편집: Jan
2015년 11월 2일
No, you can't do this in Matlab. Matlab's try, catch works exactly as explained in the documentation: doc try
댓글 수: 1
Steven Lord
2015년 11월 2일
One way to approximate Java's approach is to include a SWITCH/CASE in your CATCH block.
x = magic(2);
try
a = x+magic(3);
catch ME
switch ME.identifier
case 'MATLAB:dimagree'
% handle the 'Matrix dimensions must agree.' error
case 'MATLAB:mixedClasses'
% handle the error that occurs when you try to combine
% unlike integer types
otherwise
rethrow(ME) % I don't know what error occurred, so just rethrow it
end
end
Hossam Elshahaby
2018년 6월 26일
Hi Kim,
You can make nested try catch
x = magic(2);
try
a = x+magic(3);
catch ME
try
switch ME.identifi
if true
% code
ender
case 'MATLAB:dimagree'
% handle the 'Matrix dimensions must agree.' error
case 'MATLAB:mixedClasses'
% handle the error that occurs when you try to combine
% unlike integer types
otherwise
rethrow(ME) % I don't know what error occurred, so just rethrow it
end
catch
...
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!