# Howto ruby custom exception using the C-API

**URL:** <https://www.ruby-forum.com/t/howto-ruby-custom-exception-using-the-c-api/195973>\
**Category:** Ruby\
**Created:** [October 4, 2010, 2:07pm UTC](https://www.ruby-forum.com/t/howto-ruby-custom-exception-using-the-c-api/195973 "2010-10-04T14:07:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rantanplan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@rantanplan](https://www.ruby-forum.com/u/rantanplan)\
**Post date:** [October 4, 2010, 2:07pm UTC](https://www.ruby-forum.com/t/howto-ruby-custom-exception-using-the-c-api/195973/1 "2010-10-04T14:07:22Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hello,

I create the following code to create a custom exception  
using _three_ parameter to initialize.

Q: is this code ok?

Thanks for help…

============================================================  
#include “msgque\_ruby.h”

static VALUE cMqSException;

static ID id\_new;  
static ID id\_num;  
static ID id\_code;  
static ID id\_txt;

void NS(MqSException\_Raise) (struct MqS\* mqctx) {  
MQ\_INT num = MqErrorGetNumI (mqctx);  
MQ\_INT code = MqErrorGetCodeI (mqctx);  
MQ\_CST text = MqErrorGetText (mqctx);

VALUE exc = rb\_funcall(cMqSException, id\_new,  
3, INT2NUM(num), INT2NUM(code), rb\_str\_new2(text)  
);

rb\_exc\_raise(exc);  
}

static VALUE initialize(VALUE self, VALUE num, VALUE code, VALUE txt)  
{  
rb\_ivar\_set(self, id\_num, num);  
rb\_ivar\_set(self, id\_code, code);  
rb\_ivar\_set(self, id\_txt, txt);  
return self;  
}

void NS(MqSException\_Init) (void) {  
cMqSException = rb\_define\_class(“MqSException”, rb\_eException);

rb\_define\_method(cMqSException, “initialize”, initialize, 3);

rb\_define\_attr(cMqSException, “num”, 1, 0);  
rb\_define\_attr(cMqSException, “code”, 0, 0);  
rb\_define\_attr(cMqSException, “txt”, 1, 0);

# id\_new = rb\_intern(“new”); id\_num = rb\_intern(“@num”); id\_code = rb\_intern(“@code”); id\_txt = rb\_intern(“@txt”); }

mfg, Andreas O.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v2.0.15 (GNU/Linux)  
Comment: Using GnuPG with SUSE - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJMqb9+AAoJEGTcPijNG3/A0T8H/0v0lMeQanc1ebgR+89bqfN9  
h1d7JuznukJgmfwkmoMcMfjQTLJZl/fxJ0frq/NyZ8pHk1VYStAO/wJtAmT9cfWJ  
Pqrk8Ko4LS/aKTmfPt1WBf0zUYXrUFBZ41PbWSXlvsKCnnYFQtHSAKgnSODa238W  
H46zCTSOl3hV5iST+zigtAvXTrjNYJQoesRCc3+GBTrnNNzsyoClY4STM7vuyMrN  
wV6qHnex+NsvomF//ANw5Nk8Oeh5JyOGkOfLTCecz6I/SCWxvGR1VrLmMuyAMXQg  
Xw9puxCuYu3avIHTXJfd5lNi6o8CPlDKowgZfqA7LV6SgU3pQmGZ41nGPZnhdOs=  
=K1Hl  
-----END PGP SIGNATURE-----
