Is there a way to catch errors with out using try catch statements?

5 次查看(过去 30 天)
Hi,
I have created a GUI and deployed it. It works well but sometimes it gives error but user can't see it. Is there a way to show any kind of error to the user by using message box?
In other words I want to catch all errors with out using try catch statements because I have lots of functions in my program and writing try catch for all of them is time consuming.

采纳的回答

Jan
Jan 2014-1-12
No, writing TRY-CATCH blocks is not time-consuming. It is required to catch errors and of course you cannot catch all problems automagically.
A reliable program requires an error handling and at least including the main function should be the absolut minimum.

更多回答(1 个)

Image Analyst
Image Analyst 2014-1-12
No, you need a try catch. You don't have to have it in each and every single function. If there is an error in a function without one, then it will bubble up to the first calling routine it sees that does have one. So put this in ever function where you want to alert the user to an error:
try
% Your code goes here.
catch ME
% You get here if there was an error in this function
% or some function this function called that was unhandled.
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprint('%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by