·  PHP – Syntax – OOP – Exceptions – Throw

 

- Info:

  - You can throw Exception by using throw keyword followed by Exception Object.

  - Exception constructors is overloaded allowing you two specify two optioanl parameters: error message and code.

  - Exceptions are derived from java.lang.Exception.

 

- Example:

  - Creat PHP Console Application Test.php.

 

    Test.php

    <?php

      

      try {

      //throw new Exception();         //No parameters.

      //throw new Exception("UPS");    //One parameter             accessable with $e->getMessage()

        throw new Exception("UPS", 5); //Two parameters. Second is accessable with $e->getCode()

      }

      catch (Exception $e) {

        echo "Caught exception: \n";

        echo "Message = " . $e->getMessage() . "\n"; //First  optioanl parameter.

        echo "Code    = " . $e->getCode()    . "\n"; //Second optional parameter.              

      }

      

    ?>