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!