How do I handle BOTH MException and MSLException

6 ビュー (過去 30 日間)
Virgil Bell
Virgil Bell 2015 年 3 月 16 日
編集済み: per isakson 2015 年 4 月 3 日
I am very experienced with exceptions in C++ so the concept is nothing new to me. However. implementing it in the Mathworks world is. I have a handler function that I want to accept a generic exception object and inside the handler determine which exception ofject it is and act based on that determination. "isa" doesn't work since it returns a 1 regardless if I check it for MSLException or MException.
Bottom line, if I could figure out a way to determine if an exception object was either a MSLException or a MException then I can take it from there.
Any ideas?

採用された回答

Virgil Bell
Virgil Bell 2015 年 3 月 16 日
編集済み: per isakson 2015 年 4 月 3 日
I just figured out a workable solution.
"my_exception" is an exception object generated by my program.
if (strcmp('MSLException',class(my_exception)))
Temp_exception = MException(my_exception.identifier, my_exception.message);
Temp_exception = addCause(Temp_exception, my_exception);
% store exception info
SAVE_MY_EXCEPION(Temp_exception);
else
% it isn't so store my_exception info
SAVE_MY_EXCEPION(my_exception);
end
Sure I sacrifice the MSLException handle information, but I get to store the exception information. I'd love to create storage for MSLException objects, but I don't see how to create that object. Creating it the same way as a MException doesn't work. a = MSLException('tempidentifier', 'tempmessage'); fails.

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2015 年 3 月 16 日
編集済み: Sean de Wolski 2015 年 3 月 16 日
MSLException inherits from MException so isa will return true for both. A regular MException is not necessary an MLSException, so test this:
kdlsjfajfe % errors
ME = MException.last;
isa(ME,'MSLException')
  2 件のコメント
Virgil Bell
Virgil Bell 2015 年 3 月 16 日
Good idea but what if I throw a MException and then a MSLException? I need to handle based on the exception at hand and not based on what the previous exception was.
Sean de Wolski
Sean de Wolski 2015 年 3 月 16 日
編集済み: Sean de Wolski 2015 年 3 月 16 日
That was just for me to create one for you.
In reality
try
% Whatever you're doing
catch ME
if(isa(ME,'MSLException'))
% it is
else
% it's a regular MException
end
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction-Based Unit Tests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by