From: "Iñaki Baz Castillo" Date: 2009-10-22T21:59:07+09:00 Subject: Re: Creating and raising custom exception in Ruby C extension 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 # > > 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, ...) -- Iñaki Baz Castillo