On Wed, Oct 04, 2006 at 10:50:05PM +0900, Trans wrote: > I feel like I should know how to do this already, but... How do I get > the context in which an error was thrown? > > x = SomeClass > > def dosomething > x.bar > end > > begin > dosomething > rescue NoMethodError > p e.context #=> SomeClass (HOW?) > end > > Thanks, > T. > I don't know if there is a builtin way but: class Object alias lmc_ruby_raise raise def raise(*args) if args.length >= 2 # class + some args klass, *rem_args = args exp = klass.new(*rem_args) exp.instance_variable_set("@context", self.class) lmc_ruby_raise(exp) elsif args.length == 1 case args[0] when Class exp = args[0].new exp.instance_variable_set("@context", self.class) lmc_ruby_raise( exp ) when String exp = RuntimeError.new(args[0]) exp.instance_variable_set("@context", self.class) lmc_ruby_raise( exp ) else exp = args[0] exp.instance_variable_set("@context", self.class) lmc_ruby_raise( exp ) end end end end class Exception attr_reader :context end Totally untested of course.