开发者

ignoring errors and proceeding in ruby

whenever there is an exception call that is raised, the script terminates.

do i have to resor开发者_如何学JAVAt to putting each action ? it gets very complicated fast.....

begin
#someaction

begin
#someaction2
rescue
end
rescue
end


You could use some sort of AOP mechanism to surround every method call with exception handling code (like Aquarium: http://aquarium.rubyforge.org/), or put rescue nil after every line of code, but I'm guessing that if you need to do that, then the exceptions raised are not really signalling exceptional situations in your app (which is bad) or you want to try to continue even in a situation where there's really no point to do so (which is even worse). Anyway I'd advise you to reconsider what you really need to do, because it seems to me that you are approaching the problem in a wrong way.


It's difficult to give a specific answer because I don't know what your program does.

But in general terms, I find that the best way to deal with this is to put the code that could fail into one or more seperate methods (and perhaps the method name should reflect this).

This has a number of advantages. First of all, it means that the rest of your code doesn't have to be hedged around with exception handling; secondly, if the "dangerous" actions are carefully split up into logical groups, you may be able to do exception handling on the method, not the actual actions. Stupid example:

my_list = get_list("one") # perfectly safe method

my_list.each do |x|
    begin
        x.dangerous_file_method()  # dangerous method
    rescue
        x.status = 1
    end
end    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜