# \[C exten\] How to raise a Exception instance?

**URL:** <https://rubytalk.org/t/c-exten-how-to-raise-a-exception-instance/65451>\
**Category:** ruby-talk\
**Created:** [12 May 2012 14:30 UTC](https://rubytalk.org/t/c-exten-how-to-raise-a-exception-instance/65451 "2012-05-12T14:30:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [12 May 2012 14:30 UTC](https://rubytalk.org/t/c-exten-how-to-raise-a-exception-instance/65451/1 "2012-05-12T14:30:10Z")

</div>

Hi, in Ruby land this is easy:

> **···**
>
> -----------------------------------------------------  
> &nbsp;&nbsp;error = StandardError.new "my error message"
> 
> &nbsp;&nbsp;raise error
> 
> &nbsp;&nbsp;=\> StandardError: my error message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(backtrace..)  
> -----------------------------------------------------
> 
> How to do the same at C level having a StandardError instance stored  
> within a VALUE error?
> 
> I've looked for the definition of "raise" in Ruby 1.9.3 sources. I  
> know that using rb\_funcall for Kernel module and method "raise" could  
> work, but maybe there is something a bit better.
> 
> Thanks a lot.
> 
> --  
> Iñaki Baz Castillo  
> \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Peter\_Zotov](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Peter\_Zotov](https://rubytalk.org/u/Peter_Zotov)\
**Post date:** [12 May 2012 15:12 UTC](https://rubytalk.org/t/c-exten-how-to-raise-a-exception-instance/65451/2 "2012-05-12T15:12:45Z")

</div>

Iñaki Baz Castillo писал 12.05.2012 18:30:

> Hi, in Ruby land this is easy:
> 
> -----------------------------------------------------  
> &nbsp;&nbsp;error = StandardError.new "my error message"
> 
> &nbsp;&nbsp;raise error
> 
> &nbsp;&nbsp;=\> StandardError: my error message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(backtrace..)  
> -----------------------------------------------------
> 
> How to do the same at C level having a StandardError instance stored  
> within a VALUE error?
> 
> I've looked for the definition of "raise" in Ruby 1.9.3 sources. I  
> know that using rb\_funcall for Kernel module and method "raise" could  
> work, but maybe there is something a bit better.
> 
> Thanks a lot.

Try rb\_exc\_raise:  
&nbsp;&nbsp;&nbsp;[http://rxr.whitequark.org/mri/source/eval.c#457](http://rxr.whitequark.org/mri/source/eval.c#457)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;WBR, Peter Zotov.

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [12 May 2012 15:21 UTC](https://rubytalk.org/t/c-exten-how-to-raise-a-exception-instance/65451/3 "2012-05-12T15:21:41Z")

</div>

I checked it, but it creates a new exception and it not valid for me.  
I've a Hash with instances of custom exceptions. I access them from C  
land and want to raise exactly one of those instances (and not  
creating a new instance).

Anyhow I've already it working by using rb\_funcall for invoking  
Kernel.raise(my\_error) in C.

Thanks a lot.

> **···**
>
> 2012/5/12 Peter Zotov \<whitequark@whitequark.org\>:
> 
> > Try rb\_exc\_raise:  
> > [http://rxr.whitequark.org/mri/source/eval.c#457](http://rxr.whitequark.org/mri/source/eval.c#457)
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>
