ID:          39258
 Updated by:  [EMAIL PROTECTED]
 Reported By: Lutz dot Zetzsche at sea-rescue dot de
-Status:      Open
+Status:      Closed
 Bug Type:    Documentation problem
 PHP Version: Irrelevant
 New Comment:

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';
?>


Previous Comments:
------------------------------------------------------------------------

[2006-10-25 19:51:25] 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';
?>


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=39258&edit=1

Reply via email to