I had to set mysqli_report(MYSQLI_REPORT_ALL) at the begin of my script to be able to catch mysqli errors within the catch block of my php code.
Initially, I used the below code to throw and subsequent catch mysqli exceptions
<?php
try {
$mysqli = new mysqli('localhost','root','pwd','db');
if ($mysqli->connect_errno)
throw new Exception($mysqli->connect_error);
} catch (Exception $e) {
echo $e->getMessage();
}
I realized the exception was being thrown before the actual throw statement and hence the catch block was not being called.
My current code looks like
mysqli_report(MYSQLI_REPORT_ALL) ;
try {
$mysqli = new mysqli('localhost','root','pwd','db');
} catch (Exception $e) {
echo $e->getMessage();
}
This works fine and I'm able to trap all mysqli errors