Catch exception for fopen in PHP

Description

The following code shows how to catch exception for fopen.

Example


/* www  . j  a v  a2  s .  com*/
<?php

try {

    $fh = fopen("test.txt", "r");
    if (! $fh) {
        throw new Exception("Could not open the file!");
    }
}
catch (Exception $e) {
    echo "Error (File: ".$e->getFile().", line ".
          $e->getLine()."): ".$e->getMessage();
}


?>




















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition