Custom error handling question

I’m trying to port something I’ve done in Perl to Ruby and am getting
myself confused, or perhaps drowned in details between the various

I’m trying to write a custom error handler class.
The documentation tells me that you can extend the Error class with a
custom one that does more.

So I started with

class CustomError < StandardError

def initialise(arg1, arg2, arg3, *args)


def handle( opt1, opt2, opt3)


I then had a wrapper that wrapped around the code that did the work.

def dothefanango(context, &codeblock)
oops = context, stuff, more_stuff)
yield context
rescue CustomError …

rescue CustomError other than the one above

rescue StandardError …



In principle, all well and good. In practice the details are killing

The pickaxe book says that the rescue clause can have parameters, but
I’m also concerned with the ‘raise’ passing back information - more than
just a string.

I’m drowning in the details and don’t know what to try and experiment
with. Any advice would be appreciated.

Some enlightenment.

Firstly, the goes in the block of code that is yeilded
to, not in the wrapper.

Secondly, even though I’ve found examples in the ruby libraray and rails
of handlers beign extended and having methods of their own, I cna;'t
get it o work.

For example, in actionpack/lib/action_view/base.rb
there is

render_template(template_extension, template_source,
template_file_name, local_assigns)
rescue Exception => e
if TemplateError === e

but when I try

rescue CustomError => e
puts “Caught #{e.message.to_s}”
“#{e.setup2.to_s}”, “#{e.string3.to_s}”)

the handle method doesn’t get invoked. I don’t get an error message
about it either.

I have, of course

class CustomError < StandardError

attr_accessor :setup1, :setup2, :setup3

def initialize(setup1, setup2, setup3)
@setup1 = setup1
@setup2 = setup2
@setup3 = setup3
puts “Custom Error handler registered with #{@setup1.to_s},

def handle( handle1, handle2, handle3)
puts “Custom error handler handled”
puts “\tsetup1 = #{@setup1}”
puts “\tsetup2 = #{@setup2}”
puts “\tsetup3 = #{@setup3}”
puts “\thandle1 = #{handle1}”
puts “\thandle2 = #{handle2}”
puts “\thandle3 = #{handle3}”


Anton A. said the following on 06/12/07 01:58 PM:

On Dec 6, 2007, at 6:04 PM, Anton A. wrote:

For example, in actionpack/lib/action_view/base.rb
but when I try

rescue CustomError => e
puts “Caught #{e.message.to_s}”
“#{e.setup2.to_s}”, “#{e.string3.to_s}”)

Is the ‘puts “Caught #{e.message.to_s}”’ being called? Do you see
output from it? If the answer is NO, then my guess is that the
statement in your code which raises a CustomError is not being called.

@setup1 = setup1
 puts "\tsetup2 = #{@setup2}"
 puts "\tsetup3 = #{@setup3}"
 puts "\thandle1 = #{handle1}"
 puts "\thandle2 = #{handle2}"
 puts "\thandle3 = #{handle3}"



Regards, Morton