Suppress line number display in error message
Show older comments
Is there a way to suppress the line number display when using the error command? For example, the following statement in a function: error('ERROR: I do not like argument %s',arg); displays an extra line showing where the error line occurred. ??? Error using ==> (file name) at (line number) ERROR: I do not like argument (argument) Can one stop the first line (with three question marks) from being generated? I want to stop execution (regardless of how deeply this function is buried), but the end user doesn't need to know where the error was generated.
1 Comment
Daniel Dolan
on 6 Nov 2012
Answers (4)
Image Analyst
on 3 Nov 2012
Yes. Use try/catch and spit out whatever error message you want:
try
% Some code that throws an exception....
catch ME
errorMessage = sprintf('Error in function BlahSnafuFubar().\n\nError Message:\n%s', ME.message);
fprintf(1,'%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
2 Comments
Daniel Dolan
on 5 Nov 2012
Image Analyst
on 5 Nov 2012
No, it warns the user. But it doesn't spit out any line number. If you want to do things differently once it exits the function then you'll have to return some kind of error or status code and bubble your way back out, like Daniel says. There is a "quit" command but it also shuts down MATLAB, not just your m-file.
Daniel Shub
on 5 Nov 2012
Edited: Daniel Shub
on 5 Nov 2012
In your exact use case, where you only supply an error message and no identifier, I don't think you can. If you are willing to provide an identifier than you can do something like
throwAsCaller(MException(MSGID, 'ERROR: I do not like argument %s', arg))
This will move you one step up in the stack. In order to make it all the way to the top you would need
try
mycode;
catch ME
throwAsCaller(ME);
end
at each level.
2 Comments
Daniel Dolan
on 5 Nov 2012
Daniel Shub
on 6 Nov 2012
There seem to be three ways of dealing with it. You can wrap any user space function in the try-catch and then use a throwAsCaller to suppress the error information. Better would be to do go input argument checking on user space functions to prevent errors from occurring later. Finally you might be able to do some nasty hacking, overloading of builtin functions and meta programming to automatically uninformatively error your way out.
Arthur
on 6 Nov 2012
Use getReport instead. For instance
try
%something
catch me
errorstring= getReport(me,'basic')
uiwait(warndlg(errorstring));
end
1 Comment
Daniel Shub
on 6 Nov 2012
This has the same "problem" as all the other answers, it doesn't return you immediately to the prompt
Daniel Shub
on 6 Nov 2012
I will try this as a new answer. Just wrap everything (or almost everything) in a try-catch...
function varargout = reader(varargin)
errMsg = (inputchecker(varargin));
if ~isemtpy(errMsg)
disp(errMsg);
return;
end
try
varargout = p_reader(varargin);
catch ME
errMsg = disectME(ME);
disp(errMsg);
return;
end
end
where inputchecker checks the inputs as best as possible, p_reader is your current reader function, and disectME deals with the different errors that may happen in p_reader.
2 Comments
Daniel Dolan
on 6 Nov 2012
Daniel Shub
on 6 Nov 2012
It only has to be done at the user space level. If the user only deals with the GUI, then wrap all the callbacks of the gui in a try-catch. Realistically this is a bad idea. You will make your code completely unmaintainable since you will have no idea where errors are occurring. You need to put the checks in to prevent errors from happening.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!