mirror of
https://github.com/php/php-src.git
synced 2024-11-23 18:04:36 +08:00
Allow to throw instances of classes derived from exception
This commit is contained in:
parent
aa632187cd
commit
4e5e7502b8
@ -136,18 +136,27 @@ ZEND_API zend_class_entry *zend_exception_get_default(void)
|
||||
return default_exception_ptr;
|
||||
}
|
||||
|
||||
ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC)
|
||||
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
|
||||
{
|
||||
zval *ex;
|
||||
|
||||
MAKE_STD_ZVAL(ex);
|
||||
object_init_ex(ex, default_exception_ptr);
|
||||
if (exception_ce) {
|
||||
if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
|
||||
zend_error(E_NOTICE, "Exceptions must be derived from exception");
|
||||
exception_ce = default_exception_ptr;
|
||||
}
|
||||
} else {
|
||||
exception_ce = default_exception_ptr;
|
||||
}
|
||||
object_init_ex(ex, exception_ce);
|
||||
|
||||
|
||||
if (message) {
|
||||
zend_update_property_string(default_exception_ptr, ex, "message", sizeof("message")-1, message TSRMLS_CC);
|
||||
zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
|
||||
}
|
||||
if (code) {
|
||||
zend_update_property_long(default_exception_ptr, ex, "code", sizeof("code")-1, code TSRMLS_CC);
|
||||
zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
|
||||
}
|
||||
|
||||
EG(exception) = ex;
|
||||
|
@ -13,6 +13,7 @@
|
||||
| license@zend.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Authors: Sterling Hughes <sterling@php.net> |
|
||||
| Marcus Boerger <helly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
@ -24,10 +25,15 @@
|
||||
BEGIN_EXTERN_C()
|
||||
|
||||
ZEND_API zend_class_entry *zend_exception_get_default(void);
|
||||
ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC);
|
||||
ZEND_API void zend_register_default_classes(TSRMLS_D);
|
||||
|
||||
/* exception_ce NULL or zend_exception_get_default() or a derived class
|
||||
* message NULL or the message of the exception */
|
||||
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
|
||||
|
||||
/* show an exception using zend_error(E_ERROR,...) */
|
||||
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
|
||||
|
||||
|
||||
END_EXTERN_C()
|
||||
|
||||
#endif
|
||||
|
@ -136,18 +136,27 @@ ZEND_API zend_class_entry *zend_exception_get_default(void)
|
||||
return default_exception_ptr;
|
||||
}
|
||||
|
||||
ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC)
|
||||
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
|
||||
{
|
||||
zval *ex;
|
||||
|
||||
MAKE_STD_ZVAL(ex);
|
||||
object_init_ex(ex, default_exception_ptr);
|
||||
if (exception_ce) {
|
||||
if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
|
||||
zend_error(E_NOTICE, "Exceptions must be derived from exception");
|
||||
exception_ce = default_exception_ptr;
|
||||
}
|
||||
} else {
|
||||
exception_ce = default_exception_ptr;
|
||||
}
|
||||
object_init_ex(ex, exception_ce);
|
||||
|
||||
|
||||
if (message) {
|
||||
zend_update_property_string(default_exception_ptr, ex, "message", sizeof("message")-1, message TSRMLS_CC);
|
||||
zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
|
||||
}
|
||||
if (code) {
|
||||
zend_update_property_long(default_exception_ptr, ex, "code", sizeof("code")-1, code TSRMLS_CC);
|
||||
zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
|
||||
}
|
||||
|
||||
EG(exception) = ex;
|
||||
|
@ -13,6 +13,7 @@
|
||||
| license@zend.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Authors: Sterling Hughes <sterling@php.net> |
|
||||
| Marcus Boerger <helly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
@ -24,10 +25,15 @@
|
||||
BEGIN_EXTERN_C()
|
||||
|
||||
ZEND_API zend_class_entry *zend_exception_get_default(void);
|
||||
ZEND_API void zend_throw_exception(char *message, long code TSRMLS_DC);
|
||||
ZEND_API void zend_register_default_classes(TSRMLS_D);
|
||||
|
||||
/* exception_ce NULL or zend_exception_get_default() or a derived class
|
||||
* message NULL or the message of the exception */
|
||||
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
|
||||
|
||||
/* show an exception using zend_error(E_ERROR,...) */
|
||||
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
|
||||
|
||||
|
||||
END_EXTERN_C()
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user