注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

Matlab用try catch处理异常情况Error和Warning  

2015-10-23 21:57:29|  分类: 学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
A not-well-known performance improvement trick for catching errors is to place the entire code section within a try-catch block. This is more efficient than constantly checking for some condition. For example:
% Standard code
for loopIndex = 1 : N
   refIndex = someCalculation();
   if refIndex > 0
      A(loopIndex) = B(refIndex);
   else
      A(loopIndex) = loopIndex;
   end
end
 
% Faster code
for loopIndex = 1 : N
   refIndex = someCalculation();
   try
      A(loopIndex) = B(refIndex);
   catch
      A(loopIndex) = loopIndex;
   end
end

Trapping warnings

However, what should we do in case the checked condition is meant to prevent a warning (e.g., file-access warnings) rather than an error? It would make sense to use the same exception-handling trick, but unfortunately warnings do not normally raise a trappable exception.


% Set a couple of warnings to temporarily issue errors (exceptions)
s = warning('error', 'MATLAB:DELETE:Permission');
warning('error', 'MATLAB:DELETE:FileNotFound');
 
% Run the processing section
filesList = {'a.doc', 'b.doc', 'c.doc'};
for fileIndex = 1 : length(filesList)
   try
      % Regular processing part
      fileToDelete = filesList{fileIndex};
      delete(fileToDelete);
   catch
      % Exception-handling part
      fprintf('Can''t delete %s (reason: %s)\n', fileToDelete, lasterr);
   end
end
 
% Restore the warnings back to their previous (non-error) state
warning(s);

Trapping specific warning IDs

How can we know the specific warning IDs (e.g., ‘MATLAB:DELETE:FileNotFound’) to use? The answer is to call warning(‘on’,'verbose’) and then simulate the warning. For example:

>> warning on verbose
 
>> delete sadfsefgsdfg
Warning: File 'sadfsefgsdfg' not found.
(Type "warning off MATLAB:DELETE:FileNotFound" to suppress this warning.)

Within the exception-handling part, we could check the specific exception that was thrown and possibly act differently. For example:

try
   % Regular processing part
   fileToDelete = filesList{fileIndex};
   delete(fileToDelete);
catch
   % Exception-handling part
   err = lasterror;
   switch err.identifier
      case 'MATLAB:DELETE:Permission'
         fprintf('Can''t delete %s (reason: no permission)\n', fileToDelete);
      case 'MATLAB:DELETE:FileNotFound'
         fprintf('Can''t delete %s (reason: file not found)\n', fileToDelete);
      otherwise
         fprintf('Can''t delete %s (reason: %s)\n', fileToDelete, lasterr);
   end
end

Note that within the exception-handling part, I used the lasterr and lasterror functions, rather than lastwarn. The reason is that the warnings have been converted into standard errors, and are no longer even reported by lastwarn.


----------
例1:不管什么出现什么错误,均采用catch块的措施
n = 2; k = 5; % robust combine
try 
   com = factorial(n)/(factorial(k)*factorial(n-k));
catch
   com = factorial(k)/(factorial(n)*factorial(k-n));
end
n = 5; k = 2;
combine = nchoosek(n, k)

例2:对错误进行精细化处理
A = rand(3);
B = ones(5);

try
   C = [A; B];
catch err
err.identifier
peaks

end  % end try/catch
  评论这张
 
阅读(338)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016