Creating and raising custom exception in Ruby C extension

Discussion in 'Ruby' started by Iñaki Baz Castillo, Oct 22, 2009.

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

    VALUE class_standard_error =3D rb_const_get(rb_cObject, rb_intern("Standa=
    rdError"));
    VALUE class_custom_error =3D 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.

    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #1
    1. Advertising

  2. El Jueves, 22 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
    > Hi, I'm trying to create a CustomError exception in a Ruby C extension and
    > raise it:
    >=20
    > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > rb_intern("StandardError")); VALUE class_custom_error =3D
    > rb_define_class_under(class_standard_error, "ClassError", rb_cObject);
    > rb_raise(class_custom_error, "Oh, a custom error occurred !!!");
    >=20
    > Unfortunatelly when running it I get:
    >=20
    > 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
    >=20
    > Being "my_function" the Ruby method calling to the above C code.
    >=20
    > I suspect that the line:
    > rb_raise(class_custom_error, "Oh, a custom error occurred !!!");
    > is not correct. How should look the first argument?
    >=20
    > Thanks a lot.


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

    =E2=80=93 rb_raise(VALUE exception_object, const char* format_string, ...)

    So I must use "rb_class_new_instance", am I right?



    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #2
    1. Advertising

  3. El Jueves, 22 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
    > Hi, I'm trying to create a CustomError exception in a Ruby C extension and
    > raise it:
    >=20
    > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > rb_intern("StandardError")); VALUE class_custom_error =3D
    > 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=
    =20
    than a be under it.


    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #3
  4. El Jueves, 22 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
    > Hi, I'm trying to create a CustomError exception in a Ruby C extension and
    > raise it:
    >=20
    > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > rb_intern("StandardError")); VALUE class_custom_error =3D
    > rb_define_class_under(class_standard_error, "ClassError", rb_cObject);
    > rb_raise(class_custom_error, "Oh, a custom error occurred !!!");
    >=20
    > Unfortunatelly when running it I get:
    >=20
    > 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
    >=20
    > Being "my_function" the Ruby method calling to the above C code.
    >=20
    > 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 =3D rb_const_get(rb_cObject, rb_intern("Standar=
    dError"));
    VALUE argv[0];
    VALUE new_error =3D 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.

    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #4
  5. El Jueves, 22 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
    > El Jueves, 22 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
    > > Hi, I'm trying to create a CustomError exception in a Ruby C extension
    > > and raise it:
    > >
    > > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > > rb_intern("StandardError")); VALUE class_custom_error =3D
    > > 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?

    >=20
    > Let's try the following real code:
    >=20
    > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > rb_intern("StandardError")); VALUE argv[0];
    > VALUE new_error =3D rb_class_new_instance(0, argv, class_standard_error);
    > rb_raise(new_error, "Oh an error ocurred !!!");
    >=20
    > When calling the function containing it from Ruby I get:
    >=20
    > NoMethodError: undefined method `new' for #<StandardError: StandardErro=

    r>
    >=20
    > 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 a=
    s=20
    first argument) cannot be a class instance.

    API:
    rb_raise(VALUE exception_object, const char* format_string, ...)

    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #5
  6. On Thu, Oct 22, 2009 at 3:55 PM, I=C3=B1aki Baz Castillo <> wr=
    ote:
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0VALUE class_standard_error =3D rb_const_get(rb=

    _cObject, rb_intern("StandardError"));
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0VALUE argv[0];
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0VALUE new_error =3D rb_class_new_instance(0, a=

    rgv, class_standard_error);
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0rb_raise(new_error, "Oh an error ocurred !!!")=

    ;
    >


    why not simple:

    VALUE custom_error =3D rb_define_class("CustomError", rb_eStandardError);
    rb_raise(custom_error, "An error occured!");
     
    Nikolai Lugovoi, Oct 22, 2009
    #6
  7. El Jueves, 22 de Octubre de 2009, Nikolai Lugovoi escribi=C3=B3:
    > On Thu, Oct 22, 2009 at 3:55 PM, I=C3=B1aki Baz Castillo <> =

    wrote:
    > > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > > rb_intern("StandardError")); VALUE argv[0];
    > > VALUE new_error =3D rb_class_new_instance(0, argv,
    > > class_standard_error); rb_raise(new_error, "Oh an error ocurred !!!");

    >=20
    > why not simple:
    >=20
    > VALUE custom_error =3D 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=20
    "VALUE class".

    Let me try :)

    Really thanks a lot.

    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #7
  8. El Jueves, 22 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
    > El Jueves, 22 de Octubre de 2009, Nikolai Lugovoi escribi=C3=B3:
    > > On Thu, Oct 22, 2009 at 3:55 PM, I=C3=B1aki Baz Castillo <=

    > wrote:
    > > > VALUE class_standard_error =3D rb_const_get(rb_cObject,
    > > > rb_intern("StandardError")); VALUE argv[0];
    > > > VALUE new_error =3D rb_class_new_instance(0, argv,
    > > > class_standard_error); rb_raise(new_error, "Oh an error ocurred !!!");

    > >
    > > why not simple:
    > >
    > > VALUE custom_error =3D rb_define_class("CustomError", rb_eStandardError=

    );
    > > rb_raise(custom_error, "An error occured!");

    >=20
    > Thanks, I've realized right now that first parameter in "rb_raise" can be
    > "VALUE class".
    >=20
    > Let me try :)
    >=20
    > Really thanks a lot.
    >


    Done thanks to you:

    VALUE class_xdms_error =3D rb_define_class("XDMSError", rb_eStandardError);
    VALUE class_xdms_url_parsing_error =3D rb_define_class("XDMSURLParsingErro=
    r",=20
    class_xdms_error);
    rb_raise(class_xdms_url_parsing_error, "An error occured!");

    :)


    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Oct 22, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Guetttler
    Replies:
    1
    Views:
    608
    Thomas Guetttler
    Sep 10, 2003
  2. Calvin Spealman

    Ideas for yielding and exception raising

    Calvin Spealman, Jun 5, 2004, in forum: Python
    Replies:
    3
    Views:
    327
    Peter Hansen
    Jun 8, 2004
  3. SM
    Replies:
    1
    Views:
    337
    Duncan Booth
    Oct 21, 2004
  4. leo
    Replies:
    8
    Views:
    397
    Tom Anderson
    Oct 5, 2005
  5. Iñaki Baz Castillo
    Replies:
    2
    Views:
    238
    Iñaki Baz Castillo
    Apr 20, 2011
Loading...

Share This Page