Home » Php » php – What class entries should I use to throw non-default exceptions from a C extension?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&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");