I am working on table that prints addresses from a MySQL database. I’m not sure I understand try/catch blocks very well. Old records have no IP address but new records do have an IP address in the table. The new records (with an IP address) print out fine, but only if I put it in a try catch like below:
try {
echo inet_ntop($row['ip']);
}
catch (Exception $e){
//echo 'Exception caught: ', $e->getMessage(), "\n";
echo "n/a";
}
The records that don’t have an IP in the IP field print out an ugly error. As show above, I commented out the error, but it prints an error anyway. How can I properly print a table full of the present IP addresses (or lack 0f) without having to look at all of these errors:
Warning: inet_ntop() [function.inet-ntop]: Invalid in_addr value in/home/zp/public_html/example.COM/example.php on line 57
Warningsare notcatchable.try..catchblocks work on Exceptions only. Warnings are a different error reporting mechanism entirely. To suppress warnings, you can use the error control operator:Of course, you should avoid warnings altogether by validating the values you pass into
inet_ntopfirst. At the very least: