--- /dev/null
+--TEST--
+Setting previous exception
+--FILE--
+<?php
+
+try {
+ try {
+ throw new Exception("First", 1, new Exception("Another", 0, NULL));
+ }
+ catch (Exception $e) {
+ throw new Exception("Second", 2, $e);
+ }
+}
+catch (Exception $e) {
+ throw new Exception("Third", 3, $e);
+}
+
+?>
+===DONE===
+--EXPECTF--
+Fatal error: Uncaught exception 'Exception' with message 'Another' in %sexception_007.php:%d
+Stack trace:
+#0 {main}
+
+Next exception 'Exception' with message 'First' in %sexception_007.php:%d
+Stack trace:
+#0 {main}
+
+Next exception 'Exception' with message 'Second' in %sexception_007.php:%d
+Stack trace:
+#0 {main}
+
+Next exception 'Exception' with message 'Third' in %sexception_007.php:%d
+Stack trace:
+#0 {main}
+ thrown in %sexception_007.php on line %d
static zend_object_handlers default_exception_handlers;
ZEND_API void (*zend_throw_exception_hook)(zval *ex TSRMLS_DC);
+void zend_exception_set_previous(zval *add_previous TSRMLS_DC)
+{
+ zval *exception = EG(exception), *previous;
+
+ if (exception == add_previous || !add_previous) {
+ return;
+ }
+ if (Z_TYPE_P(add_previous) != IS_OBJECT && !instanceof_function(Z_OBJCE_P(add_previous), default_exception_ce TSRMLS_CC)) {
+ zend_error(E_ERROR, "Cannot set non exception as previous exception");
+ return;
+ }
+ if (!exception) {
+ EG(exception) = add_previous;
+ return;
+ }
+ while (exception && exception != add_previous && Z_OBJ_HANDLE_P(exception) != Z_OBJ_HANDLE_P(add_previous)) {
+ previous = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 1 TSRMLS_CC);
+ if (Z_TYPE_P(previous) == IS_NULL) {
+ zend_update_property(default_exception_ce, exception, "previous", sizeof("previous")-1, add_previous TSRMLS_CC);
+ return;
+ }
+ exception = previous;
+ }
+}
+
void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {{{ */
{
if (exception != NULL) {
- if (EG(exception)) {
- zend_update_property(default_exception_ce, exception, "previous", sizeof("previous")-1, EG(exception) TSRMLS_CC);
- }
+ zval *previous = EG(exception);
EG(exception) = exception;
+ zend_exception_set_previous(previous TSRMLS_CC);
}
if (!EG(current_execute_data)) {
zend_error(E_ERROR, "Exception thrown without a stack frame");
zval *object, *previous = NULL;
int argc = ZEND_NUM_ARGS(), message_len;
- if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|slO", &message, &message_len, &code, &previous, default_exception_ce) == FAILURE) {
- zend_error(E_ERROR, "Wrong parameters for Exception([string $exception [, long $code ]])");
+ if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|slO!", &message, &message_len, &code, &previous, default_exception_ce) == FAILURE) {
+ zend_error(E_ERROR, "Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])");
}
object = getThis();
zval *object, *previous = NULL;
int argc = ZEND_NUM_ARGS(), message_len, filename_len;
- if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|sllslO", &message, &message_len, &code, &severity, &filename, &filename_len, &lineno, &previous, default_exception_ce) == FAILURE) {
- zend_error(E_ERROR, "Wrong parameters for ErrorException([string $exception [, long $code, [ long $severity, [ string $filename, [ long $lineno ]]]]])");
+ if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|sllslO!", &message, &message_len, &code, &severity, &filename, &filename_len, &lineno, &previous, default_exception_ce) == FAILURE) {
+ zend_error(E_ERROR, "Wrong parameters for ErrorException([string $exception [, long $code, [ long $severity, [ string $filename, [ long $lineno [, Exception $previous = NULL]]]]]])");
}
object = getThis();
old_exception = EG(exception);
EG(exception) = NULL;
zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
- if (old_exception) {
- if (EG(exception)) {
- zend_class_entry *default_exception_ce = zend_exception_get_default(TSRMLS_C);
- zval *file = zend_read_property(default_exception_ce, old_exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
- zval *line = zend_read_property(default_exception_ce, old_exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
-
- zval_ptr_dtor(&obj);
- zval_ptr_dtor(&EG(exception));
- EG(exception) = old_exception;
- zend_error(E_ERROR, "Ignoring exception from %s::__destruct() while an exception is already active (Uncaught %s in %s on line %ld)",
- object->ce->name, Z_OBJCE_P(old_exception)->name, Z_STRVAL_P(file), Z_LVAL_P(line));
- }
- EG(exception) = old_exception;
- }
+ zend_exception_set_previous(old_exception TSRMLS_CC);
zval_ptr_dtor(&obj);
}
}