In try~catch, can I use multiple catch like JAVA?

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

回答 (2 件)

Jan
Jan 2015 年 11 月 2 日
編集済み: Jan 2015 年 11 月 2 日

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
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
Hossam Elshahaby 2018 年 6 月 26 日

2 投票

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

カテゴリ

ヘルプ センター および File ExchangeCall Java from MATLAB についてさらに検索

タグ

質問済み:

2015 年 11 月 2 日

回答済み:

2018 年 6 月 26 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by