Creating and raising custom exception in Ruby C extension

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.

···

--
Iñaki Baz Castillo <ibc@aliax.net>

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?

···

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?

Thanks a lot.

--
Iñaki Baz Castillo <ibc@aliax.net>

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:

  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>

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.

···

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?

--
Iñaki Baz Castillo <ibc@aliax.net>

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

···

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:

  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?

--
Iñaki Baz Castillo <ibc@aliax.net>

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;\);

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

Let me try :slight_smile:

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>

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

:slight_smile:

···

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

Really thanks a lot.

--
Iñaki Baz Castillo <ibc@aliax.net>