# C api, getting class

**URL:** <https://www.ruby-forum.com/t/c-api-getting-class/64639>\
**Category:** Ruby\
**Created:** [February 8, 2006, 12:43pm UTC](https://www.ruby-forum.com/t/c-api-getting-class/64639 "2006-02-08T12:43:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 8, 2006, 12:43pm UTC](https://www.ruby-forum.com/t/c-api-getting-class/64639/1 "2006-02-08T12:43:52Z")

</div>

Hi,

q: How do I throw a custom exception in the c-part of a ruby program?

I have a mixed ruby/c class and I’d like to throw an exception that is  
defined in the ruby side from the c side:

myklass.rb

class Myclass  
class UnknownFormat \< Exception  
end  
end

myklass\_c.c

…  
VALUE eUnknownFormatError;  
eUnknownFormatError=rb\_gv\_get(“Myclass::UnknownFormat”);  
rb\_raise(eUnknownFormatError,“fatal: format not found”);

This does not work. I have no idea how to proceed. Some small hints  
would probably enough to point me in the right direction. I have seen  
(pickaxe) that I can define the class in the c part with  
rb\_define\_class(), but I want to reuse the class that is already  
defined on the ruby side.

Patrick

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 8, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/c-api-getting-class/64639/2 "2006-02-08T13:17:09Z")

</div>

On 2006-02-08 20:43:21 +0900, Patrick G. wrote:

> This does not work. I have no idea how to proceed. Some small hints  
> would probably enough to point me in the right direction. I have seen  
> (pickaxe) that I can define the class in the c part with  
> rb\_define\_class(), but I want to reuse the class that is already  
> defined on the ruby side.

Use rb\_path2class(“Myclass::UnknownFormat”).

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 8, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/c-api-getting-class/64639/3 "2006-02-08T13:35:17Z")

</div>

Hello Florian,

> On 2006-02-08 20:43:21 +0900, Patrick G. wrote:
> 
> > This does not work. I have no idea how to proceed. Some small hints  
> > would probably enough to point me in the right direction. I have seen  
> > (pickaxe) that I can define the class in the c part with  
> > rb\_define\_class(), but I want to reuse the class that is already  
> > defined on the ruby side.
> 
> Use rb\_path2class(“Myclass::UnknownFormat”).

This is exactly what I was looking for; seems somewhat undocumented.

Thank you,

Patrick
