From: Robert Klemme Date: 2004-11-07T21:18:37+09:00 Subject: Re: exception extensions "Joel VanderWerf" schrieb im Newsbeitrag news:418D989C.1010303@path.berkeley.edu... > > These are some extensions to Exception that I've found useful recently. > Maybe someone else will too. > > The #details method outputs something like what ruby itself does when you > see an exception in stderr. However, it returns a string, and you can > control the threshold for removing informative lines with '...'. Also, the > output is formatted more nicely, IMO. > > The #reraise_as method is useful when you want to catch something that's > fairly opaque, like NameError, or NoMethodErr, and, using a more > descriptive and app-specific exception class, raise it again, but with the > same backtrace. This both is quite nice. But does the reraise_as really work as expected? As far as I can see the set_backtrace is rendered useless because when the secondary exception is raise the stack trace is overwritten (which is quite logical btw). With your code in x.rb I get $ ruby x.rb x.rb:44:in `bar': RuntimeError: bar problem: can't find a bar anywhere around here from x.rb:46:in `bar' from x.rb:46:in `bar' from x.rb:46:in `bar' from ... from x.rb:35:in `foo' from x.rb:50 **************************************** x.rb:27:in `reraise_as': bar problem: (FooError) can't find a bar anywhere around here from x.rb:39:in `foo' from x.rb:50 Line 27 is the one with "raise e". The output is the same if I comment line 26 ("e.set_backtrace backtrace"). Probably an approach similar to Java 1.4 exception chaining is better: there each exception has an optional "cause" (another exception) and printing of the stack trace is modified that it prints this stack trace and then the cause's trace. The nice thing about this feature is that no stack frame is printed twice, i.e. the exceptions trace is shortened because the cause will contain similar frames. Also this works recursive, i.e., if the cause has a cause itself etc. Kind regards robert