Catch Fatal Error Php
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 php try catch fatal error of this site About Us Learn more about Stack Overflow the company
Php Catchable Fatal Error
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges php catch warning Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
Php Catch Exception
Sign up PHP try/catch and fatal error up vote 9 down vote favorite 1 I'm using the following script to use a database using PHP: try{ $db = new PDO('mysql:host='.$host.';port='.$port.';dbname='.$db, $user, $pass, $options); } catch(Exception $e){ $GLOBALS['errors'][] = $e; } Now, I want to use this database handle to do a request using this code: try{ $query = $db->prepare("INSERT INTO users (...) set error handler VALUES (...);"); $query->execute(array( '...' => $..., '...' => $... )); } catch(Exception $e){ $GLOBALS['errors'][] = $e; } Here is the problem: When the connection to the DB is OK, everything works, When the connection fails but I don't use the DB, I have the $GLOBALS['errors'][] array and the script is still running afterwards, When the connection to the DB has failed, I get the following fatal error: Notice: Undefined variable: db in C:\xampp\htdocs[...]\test.php on line 32 Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs[...]\test.php on line 32 Note: Line 32 is the $query = $db->prepare(...) instruction. That is to say, the script crashes, and the try/catch seems to be useless. Do you know why this second try/catch don't works and how to solve it? Thanks for the help! EDIT: There are some really good replies. I've validated one which is not exactly what I wanted to do, but which is probably the best approach. php try-catch fatal-error share|improve this question edited Oct 17 '12 at 7:04 asked Oct 17 '12 at 6:35 Ploppe 61117 add a comment| 5 Answers
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 binary Installed as an Apache module
Php Register Shutdown Function
Session Security Filesystem Security Database Security Error Reporting Using Register Globals
Php Catch Fatal Error And Continue
User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with what is php fatal error XForms Handling 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 http://stackoverflow.com/questions/12928487/php-try-catch-and-fatal-error 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 Extensions Server Specific Extensions Session Extensions http://php.net/manual/en/language.exceptions.php Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation 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 throw an object that is not will result in a PHP Fatal Error. c
PHP with register_shutdown_function PHP 5.2.0 bring us a new function : error_get_last() It return an associative array describing the http://blog.elijaa.org/2010/04/20/handling-fatal-error-in-php-with-register_shutdown_function/ last error with keys "type", "message", "file" and "line" about the last error that occurred. We can now use it with register_shutdown_function to handle fatal error, log, redirect, html page, instead of the infamous white page. 1234567891011121314151617181920 /** * Handling fatal error * * @return void */function fatalErrorHandler(){# Getting last fatal error 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() catch fatal error 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.