Extensions Ruby C :Comment puis-je intercepter toutes les exceptions, y compris les éléments qui ne sont pas des StandardErrors ?
-
13-09-2020 - |
Question
En rubis,
begin
# ...
rescue
# ...
end
ne détectera pas les exceptions qui ne sont pas des sous-classes de StandardError
.En C,
rb_rescue(x, Qnil, y, Qnil);
VALUE x(void) { /* ... */ return Qnil; }
VALUE y(void) { /* ... */ return Qnil; }
fera la même chose.Comment puis-je rescue Exception => e
à partir d'une extension Ruby C (au lieu de simplement rescue => e
)?
La solution
Ruby a besoin de plus de documentation.J'ai dû accéder au code source de Ruby, et voici ce que j'ai trouvé :
VALUE
rb_rescue(VALUE (* b_proc)(ANYARGS), VALUE data1,
VALUE (* r_proc)(ANYARGS), VALUE data2)
{
return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError,
(VALUE)0);
}
Donc, la réponse à ma question (je suppose) serait :
rb_rescue2(x, Qnil, y, Qnil, rb_eException, (VALUE)0);
VALUE x(void) { /* ... */ return Qnil; }
VALUE y(void) { /* ... */ return Qnil; }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow