# e2mmap.rb - for ruby 1.1
# $Date: 1999/02/17 12:33:17 $
# extend Exception2MessageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# extend Exception2MessageMapper
# def_e2meggage ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# extend Exception2MessageMapper
# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
# extend Exception2MessageMapper
# Foo.Fail NewExceptionClass, arg...
# Foo.Fail ExistingExceptionClass, arg...
fail "Use Ruby 1.1" if VERSION < "1.1"
module Exception2MessageMapper
@RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
E2MM = Exception2MessageMapper
def E2MM.extend_object(cl)
cl.bind(self) unless cl == E2MM
def Raise(err = nil, *rest)
Exception2MessageMapper.Raise(self.class, err, *rest)
mod.extend Exception2MessageMapper
# rest: message arguments
def Raise(err = nil, *rest)
E2MM.Raise(self, err, *rest)
def fail(err = nil, *rest)
E2MM.Fail(self, err, *rest)
rescue E2MM::ErrNotRegisteredException
# define exception c with message m.
E2MM.def_e2message(self, c, m)
# s: superclass(default: StandardError)
# define exception named ``c'' with message m.
def def_exception(n, m, s = StandardError)
E2MM.def_exception(self, n, m, s)
# {[class, exp] => message, ...}
# E2MM.def_exception(k, e, m)
# k: class to define exception under.
# define exception c with message m.
def E2MM.def_e2message(k, c, m)
E2MM.instance_eval{@MessageMap[[k, c]] = m}
# E2MM.def_exception(k, n, m, s)
# k: class to define exception under.
# s: superclass(default: StandardError)
# define exception named ``c'' with message m.
def E2MM.def_exception(k, n, m, s = StandardError)
n = n.id2name if n.kind_of?(Fixnum)
E2MM.instance_eval{@MessageMap[[k, e]] = m}
# Fail(klass, err, *rest)
# klass: class to define exception under.
# rest: message arguments
def E2MM.Raise(klass = E2MM, err = nil, *rest)
if form = e2mm_message(klass, err)
$! = err.new(sprintf(form, *rest))
$@ = caller(1) if $@.nil?
$@.shift if $@[0] =~ /^#{Regexp.quote(__FILE__)}:/
E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
def E2MM.e2mm_message(klass, exp)
if mes = @MessageMap[[c,exp]]
m = klass.instance_eval('"' + mes + '"')
alias message e2mm_message
:ErrNotRegisteredException,
"not registerd exception(%s)")