http://php.net/manual/en/pdo.error-handling.php
PDO::ERRMODE_WARNING
In addition to setting the error code, PDO will emit a traditional E_WARNING message. This setting is useful during debugging/testing, if you just want to see what problems occurred without interrupting the flow of the application.
PDO::ERRMODE_EXCEPTION
In addition to setting the error code, PDO will throw a PDOException and set its properties to reflect the error code and error information. This setting is also useful during debugging, as it will effectively "blow up" the script at the point of the error, very quickly pointing a finger at potential problem areas in your code (remember: transactions are automatically rolled back if the exception causes the script to terminate).
Exception mode is also useful because you can structure your error handling more clearly than with traditional PHP-style warnings, and with less code/nesting than by running in silent mode and explicitly checking the return value of each database call.
However, the code:
$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '***');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->query('SET wait_timeout=1;');
sleep(2);
try {
$connection->query('SELECT 1;');
} catch (\Exception $e) {
echo sprintf('Caught %s exception: %s', get_class($e), $e->getMessage()) . PHP_EOL;
}
triggers warnings:
PHP Warning: PDO::query(): MySQL server has gone away in pdo.php on line 13
PHP Warning: PDO::query(): Error reading result set's header in pdo.php on line 13
Caught PDOException exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
IMPORTANT: The question is not about problem with MySQL server gone, but about PDO error handling.
UPDATE: Warnings triggered in all three modes: ERRMODE_SILENT, ERRMODE_WARNING, ERRMODE_EXCEPTION
PHP 7.2.1 (cli) (built: Jan 5 2018 17:34:14) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies