PHP Exceptions

PHP 5 introduces exceptions using a Java-like syntax. All exceptions extend the standard Exception class, meaning that you can create your own custom exceptions.

<?php
try { /* begin exception block */
    $fp = fopen(__FILE__, "r");
    if (!$fp) {
        /* throw exception, error occurred */
        throw new Exception('no such file', 9);
    }
} catch (Exception $e) { /* what to do on error (thrown exception) */
    echo $e->getCode(); // error code
    echo $e->getFile(); // file where exception was thrown
    echo $e->getLine(); // line on which exception was thrown
    echo $e->getMessage(); // error message
    print_r($e->getTrace()); // print backtrace
}
?>

Loading ...

Related Results :

  1. PHP Exceptions
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author