Assertions in matlab
3 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am very new to matlab, I started using since a month. I want to know how assertions are handled in matlab and how are they differ from normal languages like c, c++.
Any help is appreciated.
Thank you.
1 件のコメント
Jan
2011 年 12 月 7 日
C code looks like someone has rolled an angry armadillo over the keyboard. I would not call it "normal".
採用された回答
Walter Roberson
2011 年 12 月 7 日
In C and C++, an assertion taken usually exits the program completely. A message with the file name and line is printed out on standard error, and the abort() function is called, which sends a SIGABRT signal.
It is legal for a program to have installed a signal handler to catch SIGABRT, but in C the structure of signal handling makes it difficult for a containing routine to have a signal handler that resumes execution.
In C++, the same kind of message is printed by assert, and abort() is called, which raises SIGABRT, which it is possible to catch. However, "The program is terminated without executing destructors for objects of automatic or static storage duration, and without calling any atexit function" so assert() is a pretty hard failure mode in C++
In MATLAB, a formatted message is generated based upon further parameters, and an exception is generated. If I read the documentation correctly (but it is not clear), this exception can be caught through the try/catch mechanism. I also interpret (perhaps incorrectly) that any relevant object destructors will be executed, and that any relevant onCleanup() will be executed. If nothing catches the exception, the program will return to the MATLAB command prompt.
You said "normal languages like c, c++". Other "normal" languages may handle assertions through completely different mechanisms.
2 件のコメント
Walter Roberson
2011 年 12 月 7 日
C++ has "exceptions", which are handled by try/catch and are triggered by "throw", but in C++, assert() falls outside that mechanism.
The MATLAB equivalent to C++'s "exceptions" is try/catch and usually triggered by "error" (or an internal equivalent). However, in MATLAB, assert() is basically just another error() that offers some fancy formatting.
I have confirmed that assert() can be handled by the standard try/catch mechanism.
>> try; assert(false,'man:from:mars','I only eat guitars'); catch me; disp(me); end
MException object with properties:
identifier: 'man:from:mars'
message: 'I only eat guitars'
stack: [0x1 struct]
cause: {}
その他の回答 (1 件)
Jan
2011 年 12 月 7 日
You can try these commands to find related topics in the help texts:
lookfor assert
docsearch assert
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Scope Variables and Generate Names についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!