Try to catch vs if still in PDO and some other things

There is this question that I found:

What is the advantage of using try {} catch {} versus if {} else {}

If you can add anything to it, then please do as I am new to PDO, also what this means:

$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

The MySQL website says: โ€œprovides exceptions, not errors,โ€ but I donโ€™t understand if anyone can comment on it.

+4
source share
2 answers

Exceptions can be caught using try / catch and are classes with properties, while procedural errors cannot and are not. Intermediate errors are instead handled by PHP's built-in error handling.

You can observe the difference in behavior by manually starting them.

to throw an exception:

 throw new Exception(); 

to trigger the initial error:

 trigger_error($message, U_USER_ERROR); 

Exceptions are a bassist way of handling errors. Further information on exceptions can be found here: http://php.net/manual/en/language.exceptions.php

+4
source

Well, they are very similar. In your own PHP error handling, you can reset your own errors with trigger_error($error, [$level]) , since you can use your own exceptions with throw new MyException($error) ; you can set a default error handler with set_error_handler() , which will handle the whole PHP error (except parsing) in its own way, since you can set a default exception handler with set_exception_handler() . Both native errors and exceptions from PHP are automatically triggered / generated somehow: PHP built-in errors compiling the script, exceptions if you use certain elements, such as (PDO) or something like that.

Now try the same code with different approaches: With your own PHP errors, you can do things like:

 $db = new Database(); if ($db === NULL) { trigger_error("Cannot connect to the database", E_USER_ERROR); } $result = $db->query("UPDATE charlieiscool SET iloveunicorns = 1 WHERE userid = 1"); if (!$result) { trigger_error("Error updating table", E_USER_ERROR); } $file = 'log.php'; if (!file_exists($file) or !file_get_contents($file)) { trigger_error("$file not found", E_USER_ERROR); } require($file); 

I think this does not need any explanation. If the error is triggered, the entire script is skipped and you see the error. There are no more things you can do; you can set E_USER_ERROR or E_USER_NOTICE or E_USER_WARNING and handle them differently, but you donโ€™t have that much choice. Now consider a possible OOP approach with try{} catch() {} blocks:

 try { $db = new Database(); if (!$db) { throw new DBEx("Cannot connect to the database"); } $result = $db->query("UPDATE charlieiscool SET iloveunicorns = 1 WHERE userid = 1"); if (!$result) { throw new QueryEx("Query failed"); } } catch (PDOException $e) { echo $e->getMessage(); } catch (DBEx $e) { $e->customFunction(); } catch (QueryEx) { $e->customFunctionForQuery(); } try { $file = 'log.php'; if (!file_exists($file) or !file_get_contents($file)) { throw new FileEx("$file does not exists"); } require($file); } catch (FileEx) { $e->fileGenerate(); $e->logError(); } 

The main difference is that if the first try{} block throws an exception, the second try{} will be executed in any way. In fact, if an exception is thrown, only the rest of the script inside this try{} block will be skipped.

Another difference (which I like most) is that you can create several classes (extending mains Exception or PDOException or others) and very much customize error handling behavior. You have unlimited possibilities for customizing your classes, adding functions, editing existing ones. You can add a specific function (for example, $e->fileGenerate(); ) and call them inside the catch() {} block, where necessary.

Note also that if you want your entire script to stop if an error occurs, this means trigger_error() is required for this error; instead, if you want the error to only stop a specific block of code associated with this error, then this is the time for try and catch.

You should not use a replacement for another, you should use it next to another, evaluating each error as it is.

By the way, PDO::setAttribute() will change the default values โ€‹โ€‹and parameters in the database handler. You can, for example, change your default selection (used in PDOStatement::fetch() ) with $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); .

Literature:

+1
source

All Articles