【发布时间】:2012-03-27 11:42:51
【问题描述】:
我正在尝试在 Ruby 中使用 AOP 处理异常。我在这里使用的工具包是 Aquarium(http://aquarium.rubyforge.org/)。
我编写了一个示例代码,它将尝试映射写下来的 ApplicationController 类的所有后代(子类)。
在执行以下程序时,我得到一个 SystemStackError(我还尝试使用“ulimit -s”设置堆栈限制)。有人请帮我解决这个问题!或者任何关于映射的建议:超类的子类的所有方法都欢迎..提前致谢。
require 'aquarium'
include Aquarium::Aspects
class ApplicationController
end
class Abc < ApplicationController
def func
puts "func called"
raise Exception.new # SystemStackError is thrown before reaching place
end
end
#Dummy class
class Def < ApplicationController
end
Aspect.new :after_raising => Exception,
:in_types_and_descendents => "ApplicationController" do |jp, object, *args|
puts "Exception Handling Code"
end
a = Abc.new
a.func
【问题讨论】:
-
我不确定这是否是问题所在,但您应该提出并挽救 StandardError,因为异常可能包括相当严重的错误。
-
@Fivell:是的,可以做到。但我被要求使用 AOP 而不是传统的方式。我还通过在创建 Aspect 时添加 :method_options => :exclude_ancestor_methods 选项解决了系统堆栈错误。
-
@Aravindan.ck: 所以现在可以使用 :exclude_ancestor_methods 选项?
-
@Fivell 是的,确实如此..n 抱歉回复晚了。
标签: ruby exception-handling aop