If someone (not me) does this in an extension:
#include <ruby.h>
#include <stdio.h>
static VALUE cFoo;
static VALUE test_foo(VALUE self) {
rb_raise(cFoo, "testing...");
}
static VALUE foo_backtrace(VALUE self) {
VALUE x = rb_str_new2("this is a backtrace");
VALUE arr = rb_ary_new();
rb_ary_push(arr, x);
return arr;
}
int main(int argc, char * argv[]) {
ruby_init();
ruby_init_loadpath();
ruby_options(argc, argv);
cFoo = rb_define_class("Foo", rb_cObject);
rb_define_method(cFoo, "backtrace", foo_backtrace, 0);
rb_define_global_function("foo", test_foo, 0);
ruby_run();
}
and I try to catch this exception like this:
begin
foo()
rescue Exception
puts "caught exception: #{$!.inspect}"
end
this does not work, because a Foo is not an Exception. I can instead do
this:
exc = true
begin
foo()
exc = false
ensure
puts "caught exception: #{$!.inspect}" if exc
end
but using an ensure block to catch an exception seems unintuitive. Is
there a better solution? Should rb_raise be allowed to raise exceptions
that are not Exceptions?
Paul