Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wrap a try catch or other error handling around this
    text
    copied!<p>I'm a little new to OOP in PHP. I was putting together an ip to location code from MaxMind, and wanted to wrap some error handling around it, because if you supply it with an incorrect IP address, or the database is corrupt, it'll throw a fatal error.</p> <p>Most of my code is stock code, but I cannot get the error handling part correct.</p> <p>Here's the stock code. The idea here is to set <code>$countryCode = 'UN'</code> and <code>$countryName = 'Unknown'</code> initially, and then, if the script works, set it to <code>whatever</code> depending on the Ip address supplied.</p> <p><strong>Stock Code</strong></p> <pre><code>require_once($_SERVER['DOCUMENT_ROOT'].'\vendor\autoload.php'); use GeoIp2\Database\Reader; $countryCode = 'UN'; $countryName = 'Unknown'; $reader = new Reader($_SERVER['DOCUMENT_ROOT'].'\geoip\GeoLite2-City.mmdb'); $record = $reader-&gt;city('24.22.173.253'); $countryCode = $record-&gt;country-&gt;isoCode . '&lt;br&gt;'; // 'US' $countryName = $record-&gt;country-&gt;name . '&lt;br&gt;'; // 'United States' echo $countryCode; echo $countryName; </code></pre> <p><strong>I tried</strong></p> <pre><code>require_once($_SERVER['DOCUMENT_ROOT'].'\vendor\autoload.php'); use GeoIp2\Database\Reader; $countryCode = 'UN'; $countryName = 'Unknown'; try{ $reader = new Reader($_SERVER['DOCUMENT_ROOT'].'\geoip\GeoLite2-City.mmdb'); } catch(Exception $e) { echo 'Message: ' .$e-&gt;getMessage(); } $record = $reader-&gt;city('24.22.173.253'); $countryCode = $record-&gt;country-&gt;isoCode . '&lt;br&gt;'; // 'US' $countryName = $record-&gt;country-&gt;name . '&lt;br&gt;'; // 'United States' echo $countryCode; echo $countryName; </code></pre> <p><strong>However, this is what I'm trying to do:</strong></p> <pre><code>require_once($_SERVER['DOCUMENT_ROOT'].'\vendor\autoload.php'); use GeoIp2\Database\Reader; $countryCode = 'UN'; $countryName = 'Unknown'; **If (the below is successful -&gt; the db is successfully opened)** $reader = new Reader($_SERVER['DOCUMENT_ROOT'].'\geoip\GeoLite2-City.mmdb'); then (proceed with the below){ $record = $reader-&gt;city('24.22.173.253'); $countryCode = $record-&gt;country-&gt;isoCode . '&lt;br&gt;'; // 'US' $countryName = $record-&gt;country-&gt;name . '&lt;br&gt;'; // 'United States' } else //Send an email to the admin here echo $countryCode; echo $countryName; </code></pre> <p>I cant use if() here, how is this done using try catch? In all there's no need to halt the script. Let the default values of $countryCode and $countryName remain if the i[ detect does not succeed.</p> <p><strong>The code in the MaxMind script is done this way:</strong></p> <pre><code>if (!filter_var($ipAddress, FILTER_VALIDATE_IP)) { throw new \InvalidArgumentException( "The value \"$ipAddress\" is not a valid IP address." ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload