# Creating and raising custom exception in Ruby C extension

**URL:** <https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903>\
**Category:** ruby-talk\
**Created:** [22 October 2009 12:30 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903 "2009-10-22T12:30:10Z")\
**Posts on this page:** 8\
**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:** [22 October 2009 12:30 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/1 "2009-10-22T12:30:10Z")

</div>

Hi, I'm trying to create a CustomError exception in a Ruby C extension and raise it:

&nbsp;&nbsp;VALUE class\_standard\_error = rb\_const\_get(rb\_cObject, rb\_intern("StandardError"));  
&nbsp;&nbsp;VALUE class\_custom\_error = rb\_define\_class\_under(class\_standard\_error, "ClassError", rb\_cObject);  
&nbsp;&nbsp;rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");

Unfortunatelly when running it I get:

&nbsp;&nbsp;ArgumentError: wrong number of arguments(1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `initialize'  
&nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `new'  
&nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `my\_function'  
&nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `test\_01

Being "my\_function" the Ruby method calling to the above C code.

I suspect that the line:  
&nbsp;&nbsp;rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");  
is not correct. How should look the first argument?

Thanks a lot.

> **···**
>
> --  
> Iñaki Baz Castillo \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<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:** [22 October 2009 12:36 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/2 "2009-10-22T12:36:24Z")

</div>

By inspecting the API I've realized that first argument must be an  
instance/object of the class:

&nbsp;&nbsp;– rb\_raise(VALUE exception\_object, const char\* format\_string, ...)

So I must use "rb\_class\_new\_instance", am I right?

> **···**
>
> El Jueves, 22 de Octubre de 2009, Iñaki Baz Castillo escribió:
> 
> > Hi, I'm trying to create a CustomError exception in a Ruby C extension and  
> > raise it:
> > 
> > &nbsp;&nbsp;VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > rb\_intern("StandardError")); VALUE class\_custom\_error =  
> > rb\_define\_class\_under(class\_standard\_error, "ClassError", rb\_cObject);  
> > rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");
> > 
> > Unfortunatelly when running it I get:
> > 
> > &nbsp;&nbsp;ArgumentError: wrong number of arguments(1 for 0)  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `initialize'  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `new'  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `my\_function'  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `test\_01
> > 
> > Being "my\_function" the Ruby method calling to the above C code.
> > 
> > I suspect that the line:  
> > &nbsp;&nbsp;rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");  
> > is not correct. How should look the first argument?
> > 
> > Thanks a lot.
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>

---

<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:** [22 October 2009 12:40 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/3 "2009-10-22T12:40:13Z")

</div>

Ops, this is wrong as ClassError should be a child of StandardError rather  
than a be under it.

> **···**
>
> El Jueves, 22 de Octubre de 2009, Iñaki Baz Castillo escribió:
> 
> > Hi, I'm trying to create a CustomError exception in a Ruby C extension and  
> > raise it:
> > 
> > &nbsp;&nbsp;VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > rb\_intern("StandardError")); VALUE class\_custom\_error =  
> > rb\_define\_class\_under(class\_standard\_error, "ClassError", rb\_cObject);  
> > rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>

---

<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:** [22 October 2009 12:55 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/4 "2009-10-22T12:55:16Z")

</div>

Let's try the following real code:

&nbsp;&nbsp;VALUE class\_standard\_error = rb\_const\_get(rb\_cObject, rb\_intern("StandardError"));  
&nbsp;&nbsp;VALUE argv[0];  
&nbsp;&nbsp;VALUE new\_error = rb\_class\_new\_instance(0, argv, class\_standard\_error);  
&nbsp;&nbsp;rb\_raise(new\_error, "Oh an error ocurred !!!");

When calling the function containing it from Ruby I get:

&nbsp;&nbsp;NoMethodError: undefined method `new' for #\<StandardError: StandardError\>

What am I doing wrong?  
Thanks a lot.

> **···**
>
> El Jueves, 22 de Octubre de 2009, Iñaki Baz Castillo escribió:
> 
> > Hi, I'm trying to create a CustomError exception in a Ruby C extension and  
> > raise it:
> > 
> > &nbsp;&nbsp;VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > rb\_intern("StandardError")); VALUE class\_custom\_error =  
> > rb\_define\_class\_under(class\_standard\_error, "ClassError", rb\_cObject);  
> > rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");
> > 
> > Unfortunatelly when running it I get:
> > 
> > &nbsp;&nbsp;ArgumentError: wrong number of arguments(1 for 0)  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `initialize'  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `new'  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `my\_function'  
> > &nbsp;&nbsp;&nbsp;&nbsp;./test\_unit.rb:22:in `test\_01
> > 
> > Being "my\_function" the Ruby method calling to the above C code.
> > 
> > I suspect that the line:  
> > &nbsp;&nbsp;rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");  
> > is not correct. How should look the first argument?
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>

---

<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:** [22 October 2009 12:59 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/5 "2009-10-22T12:59:07Z")

</div>

Definitively the line  
&nbsp;&nbsp;&nbsp;rb\_raise(new\_error, "Oh an error ocurred !!!");  
is wrong. I think that "VALUE exception\_object" (as the function requires as  
first argument) cannot be a class instance.

&nbsp;&nbsp;API:  
&nbsp;&nbsp;rb\_raise(VALUE exception\_object, const char\* format\_string, ...)

> **···**
>
> El Jueves, 22 de Octubre de 2009, Iñaki Baz Castillo escribió:
> 
> > El Jueves, 22 de Octubre de 2009, Iñaki Baz Castillo escribió:  
> > \> Hi, I'm trying to create a CustomError exception in a Ruby C extension  
> > \> and raise it:  
> > \>  
> > \> VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > \> rb\_intern("StandardError")); VALUE class\_custom\_error =  
> > \> rb\_define\_class\_under(class\_standard\_error, "ClassError", rb\_cObject);  
> > \> rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");  
> > \>  
> > \> Unfortunatelly when running it I get:  
> > \>  
> > \> ArgumentError: wrong number of arguments(1 for 0)  
> > \> ./test\_unit.rb:22:in `initialize'  
> > \> ./test\_unit.rb:22:in `new'  
> > \> ./test\_unit.rb:22:in `my\_function'  
> > \> ./test\_unit.rb:22:in `test\_01  
> > \>  
> > \> Being "my\_function" the Ruby method calling to the above C code.  
> > \>  
> > \> I suspect that the line:  
> > \> rb\_raise(class\_custom\_error, "Oh, a custom error occurred !!!");  
> > \> is not correct. How should look the first argument?
> > 
> > Let's try the following real code:
> > 
> > &nbsp;&nbsp;VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > rb\_intern("StandardError")); VALUE argv[0];  
> > &nbsp;&nbsp;VALUE new\_error = rb\_class\_new\_instance(0, argv, class\_standard\_error);  
> > &nbsp;&nbsp;rb\_raise(new\_error, "Oh an error ocurred !!!");
> > 
> > When calling the function containing it from Ruby I get:
> > 
> > &nbsp;&nbsp;NoMethodError: undefined method `new' for #\<StandardError: StandardError\>
> > 
> > What am I doing wrong?
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Nikolai\_Lugovoi](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Nikolai\_Lugovoi](https://rubytalk.org/u/Nikolai_Lugovoi)\
**Post date:** [22 October 2009 13:01 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/6 "2009-10-22T13:01:16Z")

</div>

why not simple:

VALUE custom\_error = rb\_define\_class("CustomError", rb\_eStandardError);  
rb\_raise(custom\_error, "An error occured!");

> **···**
>
> On Thu, Oct 22, 2009 at 3:55 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > ```
> > VALUE class\_standard\_error = rb\_const\_get\(rb\_cObject, rb\_intern\(&quot;StandardError&quot;\)\);
> > VALUE argv\[0\];
> > VALUE new\_error = rb\_class\_new\_instance\(0, argv, class\_standard\_error\);
> > rb\_raise\(new\_error, &quot;Oh an error ocurred \!\!\!&quot;\);
> > 
> > ```

---

<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:** [22 October 2009 13:11 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/7 "2009-10-22T13:11:01Z")

</div>

Thanks, I've realized right now that first parameter in "rb\_raise" can be  
"VALUE class".

Let me try 🙂

Really thanks a lot.

> **···**
>
> El Jueves, 22 de Octubre de 2009, Nikolai Lugovoi escribió:
> 
> > On Thu, Oct 22, 2009 at 3:55 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:  
> > \> VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > \> rb\_intern("StandardError")); VALUE argv[0];  
> > \> VALUE new\_error = rb\_class\_new\_instance(0, argv,  
> > \> class\_standard\_error); rb\_raise(new\_error, "Oh an error ocurred !!!");
> > 
> > why not simple:
> > 
> > VALUE custom\_error = rb\_define\_class("CustomError", rb\_eStandardError);  
> > rb\_raise(custom\_error, "An error occured!");
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>

---

<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:** [22 October 2009 13:13 UTC](https://rubytalk.org/t/creating-and-raising-custom-exception-in-ruby-c-extension/55903/8 "2009-10-22T13:13:53Z")

</div>

Done thanks to you:

&nbsp;&nbsp;VALUE class\_xdms\_error = rb\_define\_class("XDMSError", rb\_eStandardError);  
&nbsp;&nbsp;VALUE class\_xdms\_url\_parsing\_error = rb\_define\_class("XDMSURLParsingError",  
class\_xdms\_error);  
&nbsp;&nbsp;rb\_raise(class\_xdms\_url\_parsing\_error, "An error occured!");

🙂

> **···**
>
> El Jueves, 22 de Octubre de 2009, Iñaki Baz Castillo escribió:
> 
> > El Jueves, 22 de Octubre de 2009, Nikolai Lugovoi escribió:  
> > \> On Thu, Oct 22, 2009 at 3:55 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:  
> > \> \> VALUE class\_standard\_error = rb\_const\_get(rb\_cObject,  
> > \> \> rb\_intern("StandardError")); VALUE argv[0];  
> > \> \> VALUE new\_error = rb\_class\_new\_instance(0, argv,  
> > \> \> class\_standard\_error); rb\_raise(new\_error, "Oh an error ocurred !!!");  
> > \>  
> > \> why not simple:  
> > \>  
> > \> VALUE custom\_error = rb\_define\_class("CustomError", rb\_eStandardError);  
> > \> rb\_raise(custom\_error, "An error occured!");
> > 
> > Thanks, I've realized right now that first parameter in "rb\_raise" can be  
> > "VALUE class".
> > 
> > Let me try 🙂
> > 
> > Really thanks a lot.
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>
