Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What class entries should I use to throw non-default exceptions from a C extension?

In my C extension, I can throw a PHP exception to the calling function with zend_throw_exception. The first parameter to that function is a zend_class_entry that specifies what type of exception to throw. I know from the documentation in zend_exceptions.h that I can use zend_exception_get_default() to use the default exception type.

But, it also says that I can pass a derived class. Where can I find the class entries for the derived built in exceptions, such as InvalidArgumentException?

like image 440
murgatroid99 Avatar asked Aug 06 '14 17:08

murgatroid99


1 Answers

All the exceptions are defined in source code here;

 php-5.5.15/ext/spl/spl_exceptions.h

and can be found here when you install the devel package (e.g yum install php-devel on fedora);

 /usr/include/php/ext/spl/spl_exceptions.h

and contains the following;

extern PHPAPI zend_class_entry *spl_ce_LogicException;
extern PHPAPI zend_class_entry *spl_ce_BadFunctionCallException;
extern PHPAPI zend_class_entry *spl_ce_BadMethodCallException;
extern PHPAPI zend_class_entry *spl_ce_DomainException;
extern PHPAPI zend_class_entry *spl_ce_InvalidArgumentException;
extern PHPAPI zend_class_entry *spl_ce_LengthException;
extern PHPAPI zend_class_entry *spl_ce_OutOfRangeException;

extern PHPAPI zend_class_entry *spl_ce_RuntimeException;
extern PHPAPI zend_class_entry *spl_ce_OutOfBoundsException;
extern PHPAPI zend_class_entry *spl_ce_OverflowException;
extern PHPAPI zend_class_entry *spl_ce_RangeException;
extern PHPAPI zend_class_entry *spl_ce_UnderflowException;
extern PHPAPI zend_class_entry *spl_ce_UnexpectedValueException;

and can according to the unit test be thrown like;

zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "array size cannot be less than zero");
like image 148
Soren Avatar answered Oct 19 '22 18:10

Soren