Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PHP 7:Go to the book’s web site and download the files for Example 5 -1 and 5-2.

ID: 3916610 • Letter: P

Question

PHP 7:Go to the book’s web site and download the files for Example 5 -1 and 5-2.

Adjust the testerror program to only create an error. Create an additional testexception program (with a testexception class) to throw an axception. Now adjust the handleerror program to create an instance of both programs. The handleerror program should now be able to handle errors or exceptions from either program (class).

Example 5-1. Testerror.php with error and exception-producing methods

<?php

class testerror

{

function produceerror() {

trigger_error( "User Error", E_USER_ERROR);

echo "This line will not display";

}

function throwexception() {

throw new userException("User Exception");

echo "This line will not display";

}

}

?>

Example 5-2. The handleerror.php file captures error or exception

<?php

function errorHandler($severity, $message, $file, $line) {

               throw new errorException($message, 0, $severity, $file, $line);

      }

              

class userException extends Exception { }

set_error_handler('errorHandler');

try {

require_once("e51testerror.php");

$tester = new testerror();

$tester->throwexception();

echo "This line does not display";

$tester->produceerror(); // will not execute if line 13 is executed

echo "This line does not display";

   }     

catch (errorException $e ){

               echo $e->getMessage(); }

catch (userException $e) {

    echo $e->getMessage(); }

catch (Exception $e) {

               echo $e->getMessage(); }

echo "This line will display";

?>

Explanation / Answer

Testexception.php
<?php
class testexception{
function throwexception() {
  throw new userException("User Exception");
  echo "This line will not display";
}
}
?>
Example 5-1. Testerror.php with error and exception-producing methods
<?php
class testerror
{
function produceerror() {
trigger_error( "User Error", E_USER_ERROR);
echo "This line will not display";
}
}
?>
Example 5-2. The handleerror.php file captures error or exception
<?php
function errorHandler($severity, $message, $file, $line) {
               throw new errorException($message, 0, $severity, $file, $line);
      }
              
class userException extends Exception { }
set_error_handler('errorHandler');
try {
require_once("e51testerror.php");
require_once("testexception.php");
$tester = new testerror();
$testexc = new testexception();
$testexc->throwexception();
echo "This line does not display";
$tester->produceerror(); // will not execute if line 13 is executed
echo "This line does not display";
   }     
catch (errorException $e ){
               echo $e->getMessage(); }
catch (userException $e) {
    echo $e->getMessage(); }
catch (Exception $e) {
               echo $e->getMessage(); }
echo "This line will display";
?>