php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30379 Example of Exception $e is Incorrect
Submitted: 2004-10-10 06:33 UTC Modified: 2004-10-12 00:07 UTC
From: hans at nyphp dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hans at nyphp dot com
New email:
PHP Version: OS:

 

 [2004-10-10 06:33 UTC] hans at nyphp dot com
Description:
------------
On the page that gives examples of how to use Exceptions:

http://www.php.net/manual/en/language.oop5.exceptions.php

The usage of catch() is incorrect in the examples.  This code:

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

Would echo something like:

Caught exception: Object id #3

I cannot believe this is the desired output.  This code:

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

Would provide the expected output:

Caught exception: Always throw this error

In other words, all exception code examples with echo statements of $e should contain $e->getMessage() rather than just echoing $e.

Hans




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-10 09:40 UTC] aidan@php.net
`echo $e` will call the exception classes __toString method. This will show more information than just getMessage(). This isn't a mistake.

However, the docs are not very good - I'll have a look at them today.
 [2004-10-10 18:21 UTC] hans at nyphp dot com
While the _toString() should be called, in PHP 5.0.2 echoing an object, $e of class Exception, doesn't always call the _toString() method.

So perhaps this is more than a documentation problem.  For example, given this catch{} block:


catch( Exception $e ) {

   $ref = new ReflectionObject($e);
   echo var_export($ref->getMethods(),1);

   echo "My Error: ".$e;
}

This outputs (shortended form):

array (
 ...
  8 => 
  class ReflectionMethod {
    public $name = '__toString';
    public $class = 'Exception';
  },
)

My Error: Object id #3


H
 [2004-10-11 08:12 UTC] aidan@php.net
__toString has complicated (somewhat confusing) behaviour.

echo $e; // calls __toString
echo 'text', $e; // calls __toString
echo 'text'. $e; // doesn't call __toString
echo (string) $e; // doesn't call __toString

This is documented on the __toString page:
http://www.php.net/manual/en/language.oop5.magic.php

This behaviour may change in PHP 5.1

Also note: I've updated the exceptions page to call the getMessage method, as it's more appropriate.
 [2004-10-11 15:58 UTC] hans at nyphp dot com
Thanks Aidan.  Please note, however, that this page:

http://www.php.net/manual/en/language.oop5.magic.php

Is empty.

And thanks for changing the example to getMessage().  I think depending on toString for examples can cause confusion for the reasons you mention.

Thanks again,

H
 [2004-10-12 00:07 UTC] aidan@php.net
Yep, we have not rebuilt the manual yet. It should appear in the next couple of days.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Sep 18 02:00:01 2025 UTC