# Overriding Kernel#raise

**URL:** <https://www.ruby-forum.com/t/overriding-kernel-raise/113493>\
**Category:** Ruby\
**Created:** [August 31, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/overriding-kernel-raise/113493 "2007-08-31T18:35:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sjml](https://www.ruby-forum.com/u/sjml)\
**Post date:** [August 31, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/overriding-kernel-raise/113493/1 "2007-08-31T18:35:48Z")

</div>

I’m writing a C++ program that has a Ruby interpreter embedded in it.  
I’d like to keep all Ruby-related exceptions contained within the Ruby  
runtime so that I don’t have to protect all my calls. Since native  
objects can be subclassed by Ruby, you never know when a call is going  
to result in the interpreter doing something.

To contain the exceptions, I’ve overridden Kernel#raise, like this:

module Kernel  
alias \_raise raise

def raise(\*a)  
begin  
if a.size == 0  
\_raise  
elsif a.size == 1  
\_raise(a[0])  
else  
\_raise(a[0], a[1])  
end  
rescue Exception =\> e  
$stderr.print e.class, ": ", e.message, “\n”  
$stderr.puts e.backtrace unless e.backtrace.nil?  
end  
end  
end

I can’t shake the feeling that this is _dirty_ and that there must be a  
better way to do things. Just hoping to get some feedback on both the  
concept and implementation.

---

<div class="post-metadata">

**Author:** ![sjml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sjml](https://www.ruby-forum.com/u/sjml)\
**Post date:** [August 31, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/overriding-kernel-raise/113493/2 "2007-08-31T19:45:23Z")

</div>

The splat operator is your friend:

module Kernel  
alias \_raise raise

def raise(\*a)  
begin  
\_raise(\*a)  
rescue Exception =\> e  
$stderr.print e.class, ": ", e.message, “\n”  
$stderr.puts e.backtrace unless e.backtrace.nil?  
end  
end  
end

As I’ve never done this or seen it done, I’m not sure how well it will  
work,  
but for your needs this looks fine.

Jason

---

<div class="post-metadata">

**Author:** ![sjml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sjml](https://www.ruby-forum.com/u/sjml)\
**Post date:** [August 31, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/overriding-kernel-raise/113493/3 "2007-08-31T20:03:33Z")

</div>

Whoops. Duh. 🙂

Early on in the code I hadn’t been using \*a and forgot to excise those  
remnants. Thanks for pointing it out.

Jason R. wrote:

> The splat operator is your friend:
> 
> module Kernel  
> alias \_raise raise
> 
> def raise(\*a)  
> begin  
> \_raise(\*a)  
> rescue Exception =\> e  
> $stderr.print e.class, ": ", e.message, “\n”  
> $stderr.puts e.backtrace unless e.backtrace.nil?  
> end  
> end  
> end
> 
> As I’ve never done this or seen it done, I’m not sure how well it will  
> work, but for your needs this looks fine.
> 
> Jason

---

<div class="post-metadata">

**Author:** ![sjml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sjml](https://www.ruby-forum.com/u/sjml)\
**Post date:** [September 1, 2007, 3:24am UTC](https://www.ruby-forum.com/t/overriding-kernel-raise/113493/4 "2007-09-01T03:24:39Z")

</div>

Arlen Christian Mart C. wrote:

> I’m always a bit worried when I do this that there’s a chance I’ll be  
> aliasing my method right into someone else’s aliased method. Is there  
> any way to do this ‘safely’?

Arlen makes a good point – in my case all the scripting code has been  
written from scratch by me, so I know that there are no such  
interactions. But that doesn’t mean there couldn’t be.

Any better, all-encompassing way to catch all exceptions?

---

<div class="post-metadata">

**Author:** ![sjml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sjml](https://www.ruby-forum.com/u/sjml)\
**Post date:** [September 1, 2007, 1:21am UTC](https://www.ruby-forum.com/t/overriding-kernel-raise/113493/5 "2007-09-01T01:21:50Z")

</div>

> > end  
> > end

I’m always a bit worried when I do this that there’s a chance I’ll be  
aliasing my method right into someone else’s aliased method. Is there  
any way to do this ‘safely’?

- Arlen.
