Php Set Error Handler Not Working
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Set_error_handler Fatal Error
workings and policies of this site About Us Learn more about set_exception_handler Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions php error_reporting Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Register_shutdown_function
Join them; it only takes a minute: Sign up set_error_handler Isn't Working How I Want It To Work up vote 10 down vote favorite 5 I'm taking the leap: my php scripts will ALL fail gracefully! At least, that's what I'm hoping for... I don't want to wrap (practically) every single line in try...catch statements, so I think my
Php Try Catch
best bet is to make a custom error handler for the beginning of my files. I'm testing it out on a practice page: function customError($level,$message,$file,$line,$context){ echo "Sorry, an error has occured on line $line.
"; echo "The function that caused the error says $message.
"; die(); } set_error_handler("customError"); echo($imAFakeVariable); This works fine, returning: Sorry, an error has occured on line 17. The function that caused the error says Undefined variable: imAFakeVariable. However, this setup doesn't work for undefined functions. function customError($level,$message,$file,$line,$context){ echo "Sorry, an error has occured on line $line.
"; echo "The function that caused the error says $message.
"; die(); } set_error_handler("customError"); imAFakeFunction(); This returns: Fatal error: Call to undefined function: imafakefunction() in /Library/WebServer/Documents/experimental/errorhandle.php on line 17 Why isn't my custom error handler catching undefinedd functions? Are there other problems that this will cause? Thanks, Jason php share|improve this question edited Sep 3 '08 at 16:12 Community♦ 11 asked Aug 31 '08 at 3:57 stalepretzel 5,393116184 add a comment| 6 Answers 6 active oldest votes up vote 8 down vote accepted set_error_handler
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and php error log parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling http://stackoverflow.com/questions/36621/set-error-handler-isnt-working-how-i-want-it-to-work file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time http://php.net/manual/es/function.set-error-handler.php Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box set_exception_handler » « restore_exception_handler Manual de PHP Referencia de funciones Afecta el comportamiento de PHP Manejo de errores Funciones de Manejo de Errores Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug set_error_h
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI http://php.net/manual/en/function.set-exception-handler.php binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication https://insomanic.me.uk/php-trick-catching-fatal-errors-e-error-with-a-custom-error-handler-cea2262697a2 with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference php error Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine php set error Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box trigger_error » « set_error_handler PHP Manual Function Reference Affecting PHP's Behaviour Error Handling Error Handling Functions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug set_exception_handler (PHP 5, PHP 7)set_exception_handler — Sets a user-defined exception handler function Description callable set_exception_handler ( callable $exception_handler ) Sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called. Parameters exception_handler Name of the function to be called when an uncaught exception occurs. This handler function needs to accept one parameter, which will be the exception object that was thrown. This
a custom error handlerImplementing a custom error handler using set_error_handler() in PHP can be a useful technique (Google search for more info/examples)Unfortunately, set_error_handler() doesn’t catch fatal errors — as the PHP docs say:The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.Handily, I just discovered it’s possible to catch fatal E_ERROR errors and direct them to your custom error handler using a combination of register_shutdown_function() and error_get_last():set_error_handler('myErrorHandler');register_shutdown_function('fatalErrorShutdownHandler');function myErrorHandler($code, $message, $file, $line) { ...}function fatalErrorShutdownHandler(){ $last_error = error_get_last(); if ($last_error['type'] === E_ERROR) { // fatal error myErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); }}The key is that functions registered with register_shutdown_function() are called even on a fatal error - including out of memory errors. error_get_last() can then be used to detect whether we’re ending the script because of a fatal error, and pass the error info to your custom error handler if so.UncategorizedDevPHPBlockedUnblockFollowFollowingAndy YoungStartups. Code. Product, Growth & Metrics. EIR @500startups. Previously UK @Stripe, founder @GroupSpaces.FollowAndy’s BlogEntrepreneur, engineer, music addict. Consulting on growth, product & analytics, and Distro team @ 500 Startups. Previously launched Stripe in the UK, founder of GroupSpaces, creator of Selective Tweets (#fb). Love/live in London.×Don’t miss Andy Young’s next storyBlockedUnblockFollowFollowingAndy Young