Saturday, 31 January 2015

Exception Example

<?php
// PHP 5
// try this code 
try { 
    $myfile = 'file.txt'; 

    // open file 
    if (!$file = fopen($myfile, 'r')) { 
        throw new Exception('Could not open file!'); 
    } 

    // read file contents 
    if (!$data = fread($file, filesize($myfile))) { 
        throw new Exception('Could not read file!'); 
    } 

    // close file 
    fclose($file); 

    // print file contents 
    echo $data; 

// catch errors if any 
catch (Exception $e) { 
    print 'Exception Custom Errors...'; 

?>

No comments: