SamTew SamTew - 3 years ago 171
PHP Question

Failed to catch exception in PHP

I try to use $_GET to pass the date into php else it will use current date.

try {
$selectedDate = $_GET['date'];
echo 'try';
echo $selectedDate;
} catch (Exception $e) {
$selectedDate = date('Y-m-d', time());
echo 'catch';
}


When I did not pass any input, it shows


Notice: Undefined index: date

try


Since I have use try catch block, why it would not run catch block?

BSB BSB
Answer Source

Its just a notice. You can check if it exists as below:

try {
    if(isset($_GET['date']))
      $selectedDate = $_GET['date'];
      echo 'try';
      echo $selectedDate;
  } catch (Exception $e) {
    $selectedDate = date('Y-m-d', time());
    echo 'catch';
  }

I have added if(isset($_GET['date'])) to check existence of date.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download