Php Handle Fatal Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site php register_shutdown_function About Us Learn more about Stack Overflow the company Business Learn more about
Php Catch Fatal Error And Continue
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Register_shutdown_function Fatal Error
Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Handle fatal errors
What Is Fatal Error In Php
in PHP using register_shutdown_function() up vote 22 down vote favorite 11 According to the comment on this answer it is possible to catch Fatal Errors through a shutdown function which cannot be caught using set_error_handler(). However, I couldn't find out how to determine if the shutdown has occured due to a fatal error or due to the script reaching its end. Additionally, the debug backtrace fatal error in php means functions seem to be defunct in the shutdown function, making it pretty worthless for logging the stack trace where the Fatal Error occured. So my question is: what's the best way to react on Fatal Errors (especially undefined function calls) while keeping the ability to create a proper backtrace? php error-handling fatal-error share|improve this question asked Dec 10 '10 at 15:55 ThiefMaster♦ 188k38353466 add a comment| 4 Answers 4 active oldest votes up vote 40 down vote accepted This works for me: function shutdown() { $error = error_get_last(); if ($error['type'] === E_ERROR) { // fatal error has occured } } register_shutdown_function('shutdown'); spl_autoload_register('foo'); // throws a LogicException which is not caught, so triggers a E_ERROR However, you probably know it already, but just to make sure: you can't recover from a E_ERROR in any way. As for the backtrace, you can't... :( In most cases of a fatal error, especially Undefined function errors, you don't really need it. Pinpointing the file/line where it occured is enough. The backtrace is irrelevant in that case. share|improve this answer edited Dec 10 '10 at 16:22 answered Dec 10 '10 at 16:05 netcoder 43k985123 add a comment| u
PHP with register_shutdown_function PHP 5.2.0 bring us a new function : error_get_last() It return an associative array describing the php recover from fatal error last error with keys "type", "message", "file" and "line" about php 7 catch fatal error the last error that occurred. We can now use it with register_shutdown_function to handle fatal php set_error_handler error, log, redirect, html page, instead of the infamous white page. 1234567891011121314151617181920 /** * Handling fatal error * * @return void */function fatalErrorHandler(){# Getting last http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function error$error = error_get_last();# Checking if last error is a fatal errorif(($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR)){# Here we handle the error, displaying HTML, logging, ...echo 'Sorry, a serious error has occured in ' . $error['file'];}}# Registering shutdown functionregister_shutdown_function('fatalErrorHandler'); Resources : http://www.php.net/manual/en/function.error-get-last.php http://php.net/manual/en/errorfunc.constants.php FacebookTwitter Related posts: Handle configuration in PHP with var_export() http://blog.elijaa.org/2010/04/20/handling-fatal-error-in-php-with-register_shutdown_function/ Solve Wrong permissions on configuration file, should not be world writable! error on phpMyAdmin Write file manipulation cross-platform PHP code How to regularly check for a newer version in a PHP application Solve PHP Warning : session_start() [function.session-start]: Node no longer exists 2 Responses Comments2 Pingbacks0 Suraj Kumar Adhikari says: March 5, 2014 at 2:07 AM Awesome information!!! thanks Reply James says: May 31, 2016 at 2:53 PM Good tip, thanks! Reply Leave a Reply Cancel reply Your email address will not be published. Required fields are marked *Comment Name * Email * Website Search PHP Detect WebP, JP2 / JPEG 2000 and JXR / JPEG XR browser support in PHP PHP Access Windows Files from Linux Server via smbclient & PHP PHP PHP API Client Library and Google Cloud Storage PHP Solve PHP Warning : session_start() [function.session-start]: Node no longer exists Eli © 2016. All Rights Reserved.
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 http://php.net/manual/en/language.exceptions.php 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 file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing fatal error 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 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 catch fatal error 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 Extending Exceptions » « Errors in PHP 7 PHP Manual Language Reference Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug Exceptions Table of ContentsExtending Exceptions PHP 5 has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block. The thrown object must be an instance of the Exception class or a subclass of Exception. Trying to t