- Using a line number or line marker is a fragile and inefficient way of writing code. See GOTO discussions for more information on this.
- The workspace variables can be accessed in between that line and where the error message is, either through nested functions, or (shudder) awful code written using evalin, assignin, or various other operators and apps. The point is, there is nothing to prove that those are the lines where the error occurred. Even more, that line might have correctly calculated the value but some GUI callback written by a beginner then places an incorrect value into the workspace, which is then detected by error. (This is also why assignin, etc, and poking around in other workspaces is a very bad idea).
- There is no clear interpretation of what "the line where the 'error' command is placed" means: today you might have a simple allocation of a scalar value, but tomorrow you decide to put it in another function, then move it into a separate CSV file. Where should the error be indicated?
- How can input data be an error? If you read some data into MATLAB memory then the data itself is not an "error". Errors occur because code has not been written to handle some particular data, or because it has some bugs... Data itself is NOT an error! And when I change my code to handle a specific data combination to avoid an error, does the same data still contain an "error"? Clearly the data itself was never an error, only my original code could not handle it. It would be conceptually strange to indicate the data as the error for the fact that my code was not written robustly enough to handle it.
- Where does it end? Import a scalar value from a CSV file, should the row and column be identified if you feel that the data is an "error"?
- What happens if the value is passed to another variable, or converted to another data type, or put into an array, or...? How long do we need to track this variable for? Through how many changes and calculations?
- What happens if multiple variables are required to trigger the error: do we have to track them all? There is no limit to how many this could be: perhaps all input data, imported from two thousand spreadsheets. How would you show all of them in your error message?
- Adding meta-programming makes your code complicated and inefficient. It also means that you go further away from standard best-practice, which is usually not something that beginners care about, but should.
Generate an error that refers to the line where the error occurred.
4 ビュー (過去 30 日間)
古いコメントを表示
Greetings,
I am currently in the process of adding error messages to my code. However, an issue that I cannot seem to solve is how to make my error message point to where the error occurred, and not the line where the 'error' command is placed.
E.g. the problem occurs at line 72, where the value x0 is wrong, but the check and the error message is placed at lines 106-108, as shown below
70 - % Location of the scatterer.
71 - z0 = 380;
72 - x0 = 210;
.
.
.
106 - if xsc(1) < 0 || xsc(end) > D
107 - error('Scatterer out of bounds. Redefine cross-range position.')
108 - end
Therefore, if the error message is triggered, I get the output
Error using IKMt (line 107)
Scatterer out of bounds. Redefine cross-range position.
Instead, I would like the error message to link to line 72, where x0 is inserted improperly.
Currently running Matlab R2012b.
Any help would be greatly appreciated.
1 件のコメント
Stephen23
2017 年 8 月 14 日
編集済み: Stephen23
2017 年 8 月 14 日
Consider:
採用された回答
Jan
2017 年 8 月 14 日
編集済み: Jan
2017 年 8 月 14 日
The source code is the database already to solve the problem. Because Matlab is an interpreted language, you can work in the source code directly and find the line containing the definition of the value manually. In opposite to this, compiled languages like C do not allow this directly with the executable. Therefore using Matlab's editor to examine the source code is the solution already.
Of course you can improve the error checking manually:
% MAGIC_STRING:&$/(
a = 10;
...
if a < 0 || a > D
magicError('Bad variable', mfilename('fullpath'), '&$/(')
end
and the magic error handler:
function magicError(Msg, File, Magic)
FileCont = strsplit(fileread(File), '\n');
Line = find(~cellfun('isempty', strfind(FileCont, Magic)), 1);
Cmd = sprintf('opentoline(''%s'', %.3d)', File, Line);
HRefStr = ['<a href="matlab:', Cmd, '">Edit definition</a>'];
fprintf(2, '*** Edit definition: %s\n', HRefStr);
error(Msg);
end
If this is really useful is a question of taste. I do not like such meta-programming, because Matlab's IDE offers everything which is needed already. You can search for the definition of a variable in the source code conveniently and reliably in the editor instead. Note that a typo in the magic key will confuse the system already. The same can happen, if any other programmer ignores this mechanism and inserts new definitions anywhere else. Then this magic system pretends to be smart and safe, but the occurring messages are misleading.
It is useful to separate data, code and GUIs strictly. If the shown parameters are variables, better move them to a dedicated function or MAT file. Do not let a user edit the source codes of your functions, because this is prone to typos. In the productive phase of a program, the source codes should be write-protected (or compiled) to prevent any unwanted editing.
As soon, as the the definition of the parameter is moved out of the productive code, your problem will vanish. Then:
Param = DefineParams();
if Param.a < 0 || Param.a > D
error('Bad definition of "a", please edit: %s', ...
which('DefineParams.m'));
end
0 件のコメント
その他の回答 (1 件)
Guillaume
2017 年 8 月 14 日
It may be possible to do what you want by passing to the error function a custom error structure, in which you craft the stack field to refer to the line you want. However you're trying to do something contrary to the way matlab works, so I would advise you to forget about it. The error line is the line at which the error message is generated and that's it.
You could simply change your error message to include the line that is the source of the error (e.g. error('Scatterer out of bounds. Redefine cross-range position on line 72'). That's also a dangerous thing as you can be certain that after a few edit, that error message won't refer to the correct line anymore.
In any case, where x0 is inserted improperly would indicate that you're working with a script. Use functions instead and you can craft your error message to refer to the function inputs rather than an arbitrary line number.
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!