php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #39258 Addition of lacking basic information to the Exceptions documentation page
Submitted: 2006-10-25 19:51 UTC Modified: 2007-08-17 12:31 UTC
From: Lutz dot Zetzsche at sea-rescue dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2006-10-25 19:51 UTC] Lutz dot Zetzsche at sea-rescue dot de
Description:
------------
The introductory page about exceptions in PHP5 is lacking some basic information with some good, ready-to-use examples:

1. Throwing an exception and catching an exception are two separate things. The exception can be caught everywhere in the code.

2. The exception is passed through the call stack, until the exception is caught or a fatal error occurs.

This information is part of an user contributed note from 06-May-2005 07:15, but is vital enough to appear in the documentation itself.

Reproduce code:
---------------
http://www.php.net/manual/en/language.exceptions.php
http://www.php.net/manual/en/language.exceptions.php#52612

Expected result:
----------------
<?php
function a($x) {
  echo 'Calling function a($x), $x='.$x."\n";
  if ($x < 0) {
    $error = 'Error: $x < 0';
    throw new Exception($error);
  }
}

function b($y) {
  echo 'Calling function b($x), $y='.$y."\n";
  a($y);
}

try {
  b(-10);
  echo('OK! No exception caught.'."\n\n");
} catch (Exception $e) {
  echo 'Caught exception: ',  $e->getMessage(), "\n\n";
}

try {
  b(10);
  echo('OK! No exception caught.'."\n\n");
} catch (Exception $e) {
  echo 'Caught exception: ',  $e->getMessage(), "\n\n";
}

// Continue execution
echo 'Hello World';
?>

Actual result:
--------------
<?php
try {
   $error = 'Always throw this error';
   throw new Exception($error);

   // Code following an exception is not executed.
   echo 'Never executed';

} catch (Exception $e) {
   echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-17 12:31 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 22:01:31 2025 UTC