# Creating and raising custom exception in Ruby C extension

**URL:** <https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712>\
**Category:** Ruby\
**Created:** [October 22, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712 "2009-10-22T14:31:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 2:31pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/1 "2009-10-22T14:31:02Z")

</div>

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?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 2:36pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/2 "2009-10-22T14:36:53Z")

</div>

El Jueves, 22 de Octubre de 2009, IÃ±aki Baz C. escribiÃ³:

> ArgumentError: wrong number of arguments(1 for 0)
> 
> Thanks a lot.

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

â€“ rb\_raise(VALUE exception\_object, const char\* format\_string, …)

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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/3 "2009-10-22T14:40:59Z")

</div>

El Jueves, 22 de Octubre de 2009, IÃ±aki Baz C. 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 !!!”);

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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/4 "2009-10-22T14:55:38Z")

</div>

El Jueves, 22 de Octubre de 2009, IÃ±aki Baz C. escribiÃ³:

> 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:

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 !!!”);

When calling the function containing it from Ruby I get:

NoMethodError: undefined method `new’ for #\<StandardError:  
StandardError\>

What am I doing wrong?  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/5 "2009-10-22T15:02:04Z")

</div>

On Thu, Oct 22, 2009 at 3:55 PM, IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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!”);

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/6 "2009-10-22T14:59:43Z")

</div>

El Jueves, 22 de Octubre de 2009, IÃ±aki Baz C. escribiÃ³:

> > is not correct. How should look the first argument?  
> > NoMethodError: undefined method `new’ for #\<StandardError: StandardError\>
> 
> What am I doing wrong?

Definitively the line  
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.

API:  
rb\_raise(VALUE exception\_object, const char\* format\_string, …)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/7 "2009-10-22T15:11:26Z")

</div>

El Jueves, 22 de Octubre de 2009, Nikolai L. escribiÃ³:

> On Thu, Oct 22, 2009 at 3:55 PM, IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 22, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/creating-and-raising-custom-exception-in-ruby-c-extension/177712/8 "2009-10-22T15:14:22Z")

</div>

El Jueves, 22 de Octubre de 2009, IÃ±aki Baz C. escribiÃ³:

> > 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.

Done thanks to you:

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

🙂
