Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP try/catch and fatal error

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 (...) 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.

like image 297
Ploppe Avatar asked Oct 17 '12 06:10

Ploppe


People also ask

Can I catch fatal error PHP?

You can "catch" these "fatal" errors by using set_error_handler() and checking for E_RECOVERABLE_ERROR. I find it useful to throw an Exception when this error is caught, then you can use try/catch.

What is a fatal error PHP?

Fatal Error Fatal errors are ones that crash your program and are classified as critical errors. An undefined function or class in the script is the main reason for this type of error. There are three (3) types of fatal errors: Startup fatal error (when the system can't run the code at installation)

Does PHP have try catch?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.


1 Answers

try/catch blocks only work for thrown exceptions (throw Exception or a subclass of Exception must be called). You cannot catch fatal errors using try/catch.

If your DB connection cannot be established, I would consider it fatal since you probably need your DB to do anything meaningful on the page.

PDO will throw an exception if the connection cannot be established. Your specific problem is that $db is not defined when you try to call a method with it so you get a null pointer (sort of) which is fatal. Rather than jump through if ($db == null) hoops as others are suggesting, you should just fix your code to make sure that $db is either always defined when you need it or have a less fragile way of making sure a DB connection is available in the code that uses it.

If you really want to "catch" fatal errors, use set_error_handler, but this still stops script execution on fatal errors.

like image 51
Explosion Pills Avatar answered Sep 18 '22 12:09

Explosion Pills