php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50454 set_exception_handler to work more like set_error_handler
Submitted: 2009-12-11 20:02 UTC Modified: 2010-12-01 16:28 UTC
From: randallgirard at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.1 OS: Windows
Private report: No CVE-ID: None
 [2009-12-11 20:02 UTC] randallgirard at hotmail dot com
Description:
------------
When utilizing set_error_handler(), the error_handler leaves it to the scripter to decide whether or not script execution should continue or quit (die/exit, etc.) I believe it returns execution to the parent scope? (I'm not 100% sure, but that isn't my point)

"Execution will stop after the exception_handler is called"

I believe there should be some way to continue execution (probably in the parent scope from where the exception was thrown). Considering at the current moment (or at least the documentation does not say) the return value does nothing: What if returning (bool) TRUE (or even a value which evaluates to true) causes script execution to return to the parent scope and continue, and (bool) FALSE (or a value which evaluates to false, hence when nothing is returned) will cause the same and current result. I feel that this is the final link in the error handling routines, and am so far most satisfied with the new features in PHP 5.3.x! I am very excited about these releases!!


If there is a specific reason that we are not aloud a way to return scripting when unhandled exceptions are thrown, PLEASE indicate the reason here. It is my belief that many scripters would want to throw exceptions and act like an E_USER_NOTICE, or E_USER_WARNING, compared to error handling, that is when caught in the exception_handler... <b>Especially when you combine both of the handlers to work together, which is NOT the result I expected.</b>

Reproduce code:
---------------
function exception_handler($exception) {
  echo "Uncaught exception: " , $exception->getMessage(), "\n";
}

set_exception_handler('exception_handler');

	function errorone() {
		throw new Exception("Test 1");
	}
	function errortwo() {
		throw new Exception("Test 2");
	}
	function test() {
		errorone();
		errortwo();
	}
	test();
	test();

# I also did further tests involving class scopes that I thought might
affect this, but I'm almost positive processing of the script is
terminated without a way to restore it.


Expected result:
----------------
Uncaught exception: Test 1\nUncaught exception: Test 2\nUncaught
exception: Test 1\nUncaught exception: Test 2\n

Actual result:
--------------
Uncaught exception: Test 1\n


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-11 20:21 UTC] randallgirard at hotmail dot com
HERE is my current error handler code that I wrote yesterday?

<?php
namespace frm {
	
# ------------- ERROR HANDLING
	
	abstract class error {
		
		public static $LIST = array();
		
		public static function initiate( $log = false ) {
		# setup error handling
		# NOTE: If namespaces are used, they must be specified in the below
calls:
		#		ex: '\\my_namespace\\error::handler'
			set_error_handler( '\\frm\\error::err_handler' );
			set_exception_handler( '\\frm\\error::exc_handler' );
		# If $log is set (should be a file) then enable logging and set
ERROR_LOG
			if ( $log !== false ) {
				if ( ! ini_get('log_errors') )
					ini_set('log_errors', true);
				if ( ! ini_get('error_log') )
					ini_set('error_log', $log);
			}
		}
		
	# Error handler (catch unhandled errors)
		public static function err_handler($errno, $errstr, $errfile,
$errline, $errcontext) {
			$l = error_reporting();
			if ( $l & $errno ) {
				
			# determine error type and if we exit or not
				$exit = false;
			# The following error types are not supported because they get thrown
BEFORE RUNTIME:
			# E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
E_COMPILE_WARNING
				switch ( $errno ) {
					case E_USER_ERROR:
						$type = 'Fatal Error';
						$exit = true;
					break;
					case E_USER_WARNING:
					case E_WARNING:
						$type = 'Warning';
					break;
					case E_USER_NOTICE:
					case E_NOTICE:
				# error handling is for PHP versions < 5.2
					case @E_STRICT:
						$type = 'Notice';
					break;
					case @E_RECOVERABLE_ERROR:
						$type = 'Catchable';
					break;
				# unknown error type:
				# ...just in case a new error type is added
					default:
						$type = 'Unknown Error';
						$exit = true;
					break;
				}
				
				$exception = new \ErrorException($type.': '.$errstr, 0, $errno,
$errfile, $errline);
				
				if ( $exit ) {
				# MAKE SURE we exit termination
					exc_handler($exception);
					exit();
				}
				else
				# NOTE: Uncaught exceptions cause the script to terminate processing
and does NOT continue
					throw $exception;
			}
			return false;
		}
		
	# Exception handler - catch unhandled exceptions, perform logs, etc
		function exc_handler($exception) {
			$log = $exception->getMessage() . "\n" .
$exception->getTraceAsString() . LINEBREAK;
		# log error if enabled
			if ( ini_get('log_errors') )
				error_log($log, 0);
			print("Unhandled Exception" . (DEBUG ? " - $log" : ''));
		}
		
	}
	
}
?>

Now, if an (for example) E_USER_WARNING or E_USER_NOTICE error is
triggered without a TRY block in the parent scope then script execution
terminates and doesn't allow me to control whether or not processing is
terminated.
 [2009-12-11 21:11 UTC] randallgirard at hotmail dot com
I understand the reasoning that uncaught exceptions should not allow the termination to continue, but because of situations like the one I have presented, I believe there SHOULD be a way to return execution to the parent scope in the event that an uncaught exception arises.
 [2010-12-01 16:28 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-01 16:28 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 16:01:30 2024 UTC