How to call a function on "dbstop if error"

2 ビュー (過去 30 日間)
Matlab2010
Matlab2010 2013 年 4 月 15 日
I have a large and complex function. I set "dbstop if error" at the start of the function. If the function fails for some reason it will hit dbstop if error.
At this point I wish to call a failed function which can email me the log file and other diagnostics, as well as letting me know that failure has occurred.
How can I do this?
For example, below is a function that will fail
function myFunction()
dbstop if error;
x = 1;
y = 2;
z = xy; % I will fail. After this failure, I would like the code to call a function called myFail.m
end

採用された回答

Friedrich
Friedrich 2013 年 4 月 15 日
編集済み: Friedrich 2013 年 4 月 15 日
Hi,
why not using try/catch?
function myFunction()
try
x = 1;
y = 2;
z = xy; % I will fail. After this failure, I would like the code to call a function called myFail.m
catch err
myFail
end
  3 件のコメント
Friedrich
Friedrich 2013 年 4 月 15 日
No thats not true. First line of your code is try and at the end add the catch statement. You dont need to add this at any point where it can fail.
Matlab2010
Matlab2010 2013 年 4 月 15 日
I take back my previous comment and agree with you having read your/Jans comments. I was wrong :(.
The reasons I thought this might not work, where 1) as the code already contains a lot of try/catch, though I guess I can just have nested try/catch and 2) the code fires other instances of matlab through a batch file as worker nodes. I now track these by writing log files.
thank you for your help/ considerations.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2013 年 4 月 15 日
編集済み: Jan 2013 年 4 月 15 日
I can only confirm, that the TRY/CATCH method suggested by Friedrich is design to colve exactly your problem:
try
callYourFunction()
catch ME
% Now email the problem including a stack trace and the MException object
end
Encapsulating the function call in a TRY/CATCH block catchs all internal errors. In opposite to dbstop if error it does not impede the JIT acceleration dramatically.
If you want to enable the debugger in the editor afterwards:
try
callYourFunction()
catch ME
% Now email the problem including a stack trace and the MException object
% Restart:
dbstop if all error
callYourFunction()
end
But how useful is it to enable a remote and local debugging at the same time? It increases the chance, that the local user manipulates the bug locally, such that the (time delayed) external debugging will be based on an outdated source code already.

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by