#50446 [Opn]: Missing documentation or bug for SET_EXCEPTION_HANDLER()

From: Date: Fri, 11 Dec 2009 20:32:01 +0000
Subject: #50446 [Opn]: Missing documentation or bug for SET_EXCEPTION_HANDLER()
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-3452@lists.php.net to get a copy of this message
ID: 50446 Updated by: bjori@php.net Reported By: randallgirard at hotmail dot com Status: Open Bug Type: Documentation problem Operating System: Windows PHP Version: 5.3.1 New Comment: Reading that code without syntax highlighting and weird lineendings is very hard. However: > 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. That is exactly the point. If you do not catch exceptions then the application *MUST* die. Exception aren't regular warnings/notices. Exceptions means something very very wrong happened. If you don't catch that exception and deal with it, then the application is in a state that it simply cannot continue. Having a global exception *handler* is just a way for you to deal with the error (log it, send email/sms...). It does not replace a try/catch block. Previous Comments: ------------------------------------------------------------------------ [2009-12-11 20:21:24] 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 20:13:01] randallgirard at hotmail dot com Oops I forgot to supply the link, here ya go: http://bugs.php.net/bug.php?id=50454 ------------------------------------------------------------------------ [2009-12-11 20:11:40] randallgirard at hotmail dot com I added a new bug for feature request at the following, explaining more of a WHY reason for such a situation. Mainly in my situation though, is because I am combining SET_ERROR_HANDLER with SET_EXCEPTION_HANDLER, and the ERROR_HANDLER fires ErrorException, as noted in the documentation. Therefor, even though my ERROR_HANDLER processes the $errnum correctly, script execution still terminates when for example E_USER_NOTICE or E_USER_WARNING happens, and no way for me to write a work around for this. ------------------------------------------------------------------------ [2009-12-11 20:10:46] bjori@php.net replace that "*must*" with an RCC2119 SHOULD ------------------------------------------------------------------------ [2009-12-11 20:08:09] bjori@php.net To process *unhandled* exceptions. If your application throws an exception you don't expect, then you *must* have an exception handler that catches that exception, logs it, and prints a pretty error page for your user. Maybe we should add a tip or a better usecase/description? (re-opening) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/50446 -- Edit this bug report at http://bugs.php.net/?id=50446&edit=1

« previous php.doc.bugs (#3452) next »